How to click on link element with specific text using watir? - text

I'm not able to click on text link 'Add' using watir:
PAGE:
<div id="divAdd" style="float: right">
<a onclick="SwitchView('2')" style="color: #1B56A7; cursor: pointer;">Add</a>
</div>
Watir CODE:
browser.link(:text =>"Add").click
EXCEPTION:
Unable to locate element, using {:tag_name=>["a"], :text=>"Add"}
Please help me how to handle this?

If the page has a lot of ajax and javascript going on, you may just have to wait a little bit for the client side code to finish rendering the page after it has been loaded from the browser.
Try this
browser.link(:text =>"Add").when_present.click
If that does not work, then make sure the item is not in a frame or something..
btw, if there is more than one link on the page with the text 'Add' then you may have to specify a container outside the link that lets you identify which link you want. eg.
browser.div(id: => "divAdd").link.when_present.click
If

This would be my way of doing it.
while browser.div(:class, 'containerDIV').a(:text, 'Add').exists? do
browser.div(:class, 'containerDIV').a(:text, 'Add').click
end

Related

Button element not interactable

I've been building an automated checkout script in Python 3.8 for the following website https://fasttimes.com.au/ (site built using Magento) and all buttons, drop downs, inputs etc have functioned up until this point. At the shipping selection stage however the button responsible for saving shipping details and allowing proceeding to billing stage, according to the python console reads; "Message: element not interactable".
I have tried using execute_script to directly interact with the site, this does not yield an error and says it's completed, but within the WebDriver the button hasn't actually been clicked and so proceedings to the next step of checkout do not take place. As for 'Action Chains'. they simply yield the error above, same as finding by XPATH or CSS_Selector. I've tried using the import 'Keys' and attempting to action the button with the ENTER key, as well as using the WebDriver substitute .send_keys(u'\ue007') to no avail. Before it's mentioned below I've also tried using waits, however maybe I'm doing them wrong?
E.g
element = WebDriverWait(driver, 15).until(
ec.presence_of_element_located((By.ID, "login:guest")))
Correct me if above line is incorrect formatting
Site's HTML is as follows:
<div class="buttons-set" id="shipping-method-buttons-container">
<p class="back-link"><small>« </small>Back</p>
<button type="button" class="button" onclick="shippingMethod.save()"><span><span>Continue</span></span></button>
<span id="shipping-method-please-wait" class="please-wait" style="display:none;">
<img src="https://static.fasttimes.com.au/skin/frontend/ultimo/default/images/opc-ajax-loader.gif" alt="Loading next step..." title="Loading next step..." class="v-middle"> Loading next step... </span>
</div>
My current code is:
driver.find_element_by_xpath("//*[#id='shipping-method-buttons-container']/button").send_keys(u'\ue007')
Any suggestions would be greatly appreciated. I believe the button that I'm trying to interact with is
<button type="button" class="button" onclick="shippingMethod.save()"><span><span>Continue</span></span></button>
...but I'm not too sure. I feel like I've at this point exhausted all possible avenues, but I refuse to believe this damned button isn't intractable! Thank you:)
As you've mentioned you want to proceed to Billing stage, I understand you are at step 1 of the checkout process. Am saying this because what you have identified with xpath //*[#id='shipping-method-buttons-container']/button is the button at step 4 Shipping Method, which is indeed not intractable yet.
You need to select the Checkout Method first, and carry on from there. Give this a try:
driver.find_element_by_id('login:guest').click() # select the "Checkout as Guest" radio button
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//*[#id='onepage-guest-register-button']")))
driver.find_element_by_xpath("//*[#id='onepage-guest-register-button']").click() # click Continue
Alternatively, if it's step 4 you're really stuck at:
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//*[#id='shipping-method-buttons-container']/button")))
driver.find_element_by_xpath("//*[#id='shipping-method-buttons-container']/button").click()

link to anchor from URL using react

I have a FAQ page i have created with many questions and answers in the form of
<div>
<h4 id="anchor-name">question</h4>
<p>answer</p>
</div>
This works as expected if i have a button or clickable link in the form of
Click here to go to anchor
which takes me directly to my question with that id (expected behaviour)
The problem is when i try to access this from a URL from another tab by doing something like
https://my-app-url/#/FAQ/#anchor-name
it redirects me to my FAQ page but not to my question. Instead, it just loads the FAQ and stays on the top.
Some context:
frontend is in react
the first # in the url https://my-app-url/#/FAQ/#anchor-name is mandatory. Apparently if i just go with https://my-app-url/FAQ/#anchor-name it never loads the page
I have tried enclosing the <div> tags and the <h4> tags by an <a> tag but it didn't work. In those cases what i did was
<div>
<a href="#anchor-name">
<h4 id="anchor-name">question</h4>
<p>answer</p>
</a>
</div>
What i wish to know and cannot seem to find it by doing research a lot is:
Is it possible to access this anchor via URL from an external link by using react-router library?
Could it be that by using react the html way to anchor is not working as expected?
Is my first # in the URL interfering with the anchor linking feature?
You don't need that last / right before the #anchor-tag.
so the URL would be:
https://my-app-url/#/FAQ#anchor-name
For anyone who might be interested, I fixed this issue. The problem was fixed by replacing HashHistory with BrowserHistory. With HashHistory, the URL gets an # that interferes with anchors. By using BrowserHistory, that # is never there so the issue is gone. Hopes this helps.

Cucumber/Capybara Selecting Button from Specific Class?

So i've ran into an issue, im trying to click a button...which unfortunately has the same text (IE there are 2 buttons on the page which the same text, basically a save button)
Lets pretend the button text is just "Save".
I did notice that they had different classes.
<button data-action="submit" class="btn btn-primary btn-save">Save</button>
whereas the other button is:
<button name="button" type="submit" class="btn btn-primary set-right">
<i class="glyphicon glyphicon-floppy-disk"></i> Save
</button>
I know glypicon is just an icon set....but they both seem to belong to the same class? but have different class names? (Sorry im not familiar with Rails)
It doesn't honestly matter which one I select as they both have the same function. I've seen where you can use xpath? but aren't we supposed to use css selectors or something now? (As in thats the newest way?) I may be wrong....
Could I use something like:
find(:xpath, '//button[#class="btn-save"]').click
Im trying to avoid "rails" only solutions, as not all the websites I test on are rails based.
You have various possibilities:
1) Find the button by its class
find(button.btn.btn-primary.btn-save).click
2) Find the button by its css selector or xpath (you can copy them using Google Chrome: right click -> Inspect -> right click on your element -> copy css or xpath)
find(:css, "your button css selector").click
find(:xpath, "your button xpath").click
If both buttons really do the same thing then you could always just do
first(:button, 'Save', minimum: 1).click
which will be less prone to breakage as your page structure changes than using an xpath selector. The minimum:1 option will just make #first wait a bit until at least one button is on the page (like find would) - it may not be necessary depending on your test structure

Can not click <button> element using javascript onmousedown with Watir

Using Watir I have been able to isolate this button element:
<BUTTON id=authButton class=commandButton onmousedown="$('newOrder:hiddenAuth').click();">Authorize Payment</BUTTON>
Here is my watir code:
$browser.div(:id, "rSide:j_id750_body").table(:index, 0) [1] [0] .button(:index, 0).click
I was able to see I'm on the <button> by using watir's "flash" method to highlight the button. For some reason I am unable to actually click the button. I think it might have something to do with the onmousedown code but I really don't know. Any help would be greatly appreciated.
The button is wired up to do some specific action when it see's the onmousedown event, so if .click is not working, the next thing to try is firing that specific event, instead of using the .click method.
If that doesn't work try forcing the javascript to execute the newOrderLhiddenAuth script.
oh and FYI for your developer, they may want to get into the habit of using lower case for their HTML tags
"the World Wide Web Consortium (W3C) recommends lowercase in HTML 4,
and demands lowercase tags in XHTML."

Watir :- click an image button?

I have am using firefox and i have modified its user agent to make it behave like iphone web browser. now when i open google.com using watir and now i want to click on the searh button which is an image in mobile view. how to do that.
To click an image
<img id="logo">
try this
browser.image(:id => "logo").click
If the image is actually a button
<input type="image" id="logo">
try this
browser.button(:id => "logo").click
The next time please provide relevant HTML.
Also, take a look at HTML Elements Supported by Watir page.

Resources