Selenium webdriver can't find my button - java

This is my button link:
<a class="button" href="#" onclick="ajaxtoelement('include/system.php?mode=begin&location='+getSelectedValue('location')+'&terminallane='+getSelectedValue('terminallane')+'','keyboard')</a>"
And I have tried doing
driver.findElement(By.xpath("//a[#class='ajaxtoelement('include/system.php?mode=begin&location='+getSelectedValue('location')+'&terminallane='+getSelectedValue('terminallane')]"));
Why can't selenium still find the button I'm specifying?

It should be accessed via onclick and not class one simple way is,
driver.findElement(By.xpath("//a[contains(#onclick, 'onClickValue')]");

I don't know your HTML structure, but
How many buttons do you have?
If you only have one button you don't need xpath, use the simpler way:
driver.findElement(By.className("button"));
If there are several more buttons, but the button you described is the only one that contains for example the word "terminallane":
driver.findElement(By.xpath("//a[#class = 'button' and contains(#onclick, 'terminallane')]"));

Try with the below xpath::
//a[#href='#']
else
//a[contains(#onclick, 'ajaxtoelement('include/system.php?mode=begin&location='+getSelectedValue('location')+'&terminallane='+getSelectedValue('terminallane')+'','keyboard')')]

Related

how to find element button in div using selenium?

I have three <div class= emploBox "> and each has a button. Buttons do not have unique names. How can I find this particular button?
I want to use class=cutTooLongTest and test 'automated tester' but I don't know how.
wants to find the button marked in yellow.
enter image description here
I have no idea for a solution
If I understood you right, you need to find the XPath for the button using the text 'automated tester'.
The XPath expression should be like this:
//h2[text() = 'automated tester']//..//button[contains(#class, 'standard Button--icon')]
this should work for you:
webDriver.findElement(By.xpath("//h2[#class = 'cutTooLongTest' and text() = 'automated tester']/following-sibling::div/button[1]"));

how to click two identical attributes with Selenium WebDriver?

In firepath I saw two identical attributes, firepath has two results.
Here is the highlighted HTML code below in firebug:
<button class="list_header_search_toggle icon-search btn btn-icon table-btn-lg" style="margin-left:0px">
And below is the whole code:
<button class="list_header_search_toggle icon-search btn btn-icon table-btn-lg" style="margin-left:0px">
<span class="sr-only">Search</span>
</button>
NOTE: There is only 1 search button, I search it every where and there is only 1 but it shows two??
How to code this in selenium web driver?
The snippet from firepath:
Update:
Html code image, from firepath:
You can use XPath functions, for example:
position() returns the position of element at DOM
//button[#id='hdr_problem_task']/th[2]/button[position()=1]
last()
//button[#id='hdr_problem_task']/th[2]/button[last()]
something like first() doesn't exist, instead of this you can use index:
//button[#id='hdr_problem_task']/th[2]/button[1]
Also if button has some text you can use it as well:
//button[#id='hdr_problem_task']/th[2]/button[text()='button name']
or with contains()
//button[#id='hdr_problem_task']/th[2]/button[contains(text(), 'button name')]
UPDATE:
The button has name Search you can use XPath with - contains().
One more small suggestion, don't forget about future support. And instead of the following locator:
//*[#id='hdr_problem_task']/th[2]/button
Much better will be:
//button[#id='hdr_problem_task']/th[2]/button
You can use th tag's name attribute value in order to recognize the correct Search button, as shown below:
//th[#name='search'][1]/button/span[text()='Search']
Let me know, whether it works for you.

Cannot click on input type = "radio" that is inside a HTML label?

I have a problem where I cannot click on the input type = "radio" using the id="same-supplier-no":
<label for="same-supplier-no" ng-class="{checked: !ctrl.energyModel.BillViewModel.SameSupplier}" class="">
<input type="radio" id="same-supplier-no" name="same-supplier" ng-model="ctrl.energyModel.BillViewModel.SameSupplier" ng-value="false" ng-change="ctrl.sameSupplierValueChanged(); ctrl.analyticsProvider.sendVirtualPageView('SameSupplierNo')" class="ng-pristine ng-untouched ng-valid" value="false">
<span></span>No
</label>
In Selenium when I do:
driver.findElement(By.id("same-supplier-no")).click();
it says that "element not visible". There are previous radio buttons in the HTML that have labels on it and they have ID's for the labels (this one does not).
I just refer to the label id and click that instead and it works. But this one doesn't have an ID for the label and I want to avoid xpath usage, however, if I have no choice, then I will just select via xpath/css.
I have also tried via locator name i.e. By.className("same-supplier"); and using a For loop and if statement to store the input type=radio element, still doesn't work.
Question is: why can I not click the input type = "radio", why is in not visible to Selenium? Is it because it is nested inside a Label? If so, does anyone know how I can click it using the id or just getting to the element?
Thank you.
Note: I am using Java 7 for Selenium.
From the chat conversation I could try some stuff on the website you are using. The radio never becomes visible. As a result of this it is impossible to use the click() method on it.
The label that contains this radio is visible however. This means you could just use click() on this label.
driver.findElement(By.cssSelector("label[for='same-supplier-no']")).click();
Or if you really want to find the element using the radio you could use xpath.
driver.findElement(By.xpath("//*[#id='same-supplier-no']/parent::label")).click();
use action to click on the lable and after that use:
Actions actions = new Actions(driver);
IWebElement menuHoverLink = driver.findElement(By.XPath("//label[#for='same-supplier-no']"));
actions.moveToElement(menuHoverLink);
actions.click();
actions.perform();
driver.findElement(By.id("same-supplier-no")).click();
After you click on "find postcode" button, add below two line of code -
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(".//*[#id='same-supplier-question']/div/div/label[2]/span"))));
This is has worked at my end...
You can change the xpath from absolute to relative....

can't click on checkbox to uncheck it seem like it's hidden

the checkbox is checked be default and can't click on it to uncheck. here is my code but it came back as error saying element is not currently visible and so may not be interacted with. org.openqa.selenium.ElementNotVisibleException.
String checkboxXPath =("//input[contains(#type='checkbox',#name='key_IT_CONFIG.ios.restriction.functionality.enable.camera_checkboxVal')]");
WebElement elementToClick = driver.findElement(By.xpath(checkboxXPath));
elementToClick.click();
Website code
<input type="checkbox" class="uwp_inputCheckBox"
name="key_IT_CONFIG.ios.restriction.functionality.enable.camera_checkboxVal"
id="key_IT_CONFIG.ios.restriction.functionality.enable.camera"
value="true" dir="ltr" hierarchy="false" expand="true"
checkedval="true" uncheckedval="false"
onclick="checkboxChange('IT_CONFIG.ios.restriction.functionality.enable.camera')"
checked="checked">
whole code
whole code http://imageshack.com/a/img661/1720/SIi6Xj.png
I think you should use explicit wait until element get visible. Please check update code here and use it:
String checkboxXPath =("//input[contains(#type='checkbox',#name='key_IT_CONFIG.ios.restriction.functionality.enable.camera_checkboxVal')]");
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(checkboxXPath)));
WebElement elementToClick = driver.findElement(By.xpath(checkboxXPath));
elementToClick.click();
I have a couple suggestions. I'm not sure why your XPath is so complex when you have an ID on the element you want to click. Try this...
driver.findElement(By.id("key_IT_CONFIG.ios.restriction.functionality.enable.camera"));
I'm kinda guessing that won't work. Looking at the HTML, I see the SPAN right above the element that you want to click and it has an onclick on it. I'm guessing that if you click that, it might trigger the click of the checkbox... so let's try that...
driver.findElement(By.cssSelector("span.uwp_checkBoxSpan.uwp_checkBoxChecked"));
You might need to check my spelling on the class names... I couldn't copy/paste since it's a picture.
Since Selenium works on Javascript I would suggest you to test the checkbox clicking thing manually by entering a Javvascript. Here are the step you need to follow:
Execute the test case manually up till where your script failed.
Goto browsers developer tools option->Console. Enter a javascript
command document.getElementById('key_IT_CONFIG.ios.restriction.functionality.enable.camera').click()
If this works then there is no reason why your code shouldn't work.

How to find button element with webdriver?

I have the following code for a button :
<div class="buttons">
<button class="btn dialog-confirm btn-primary" style="margin-left: 4px;">Confirm</button>
<button class="btn dialog-cancel" style="margin-left: 4px;">Cancel</button>
</div>
There are two buttons on is Confirm and another is Cancel
I can find the button with XPath but I don't want to use XPath.
Is there another way to find the button element in this case?
I tried this:
driver.findElement(By.className("btn dialog-confirm btn-primary")).click();
It did not find the button
Thank you for your help
Just check for a single dialog-confirm class:
driver.findElement(By.className("dialog-confirm")).click();
Or, use a CSS Selector:
driver.findElement(By.cssSelector("button.dialog-confirm")).click()
Added to alecxe and master slave's answer. It would be more specific if it is clicked by the button text, which is also easier to understand. Find the snippet for button click with xpath below.
driver.findElement(By.xpath("//button[text()='Confirm']")).click();
driver.findElement(By.xpath("//button[text()='Cancel']")).click();
Other ways using cssSelector:
Use full attribute i.e.:
driver.findElement(By.cssSelector("button[class='btn dialog-confirm btn-primary']"))
Use part of attribute i.e.:
driver.findElement(By.cssSelector("button[class*='dialog-confirm']"))
vote up for alecxe, your attempt was wrong on two accounts, when matching on multiple classes you should use By.cssSelector, and when they are set on the same element, you concatenate them with a dot, like
driver.findElement(By.cssSelector(".btn.dialog-confirm.btn-primary")).click();
this worked for me:
driver.find_element_by_class_name('buyable-full-width').click();

Categories