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]"));
Related
How can i click on this button with selenium ?
<a class="_42ft _4jy0 rfloat _ohf _4jy4 _517h _51sy" role="button" href="" ajaxify="/nux/wizard/step/?action=skip" rel="async-post" id="u_9_8">İleri</a>
Something I wish I would have figured out earlier was how to create my own advanced CSS selectors here is the page that taught me, it will work in all cases assuming your element is visible in the DOM.
https://www.smashingmagazine.com/2009/08/taming-advanced-css-selectors/
For your given element you could write this many ways
Generic form
tag[attribute='ATTRIBUTE_VALUE']
For your example
a[id='u_9_8']
or
a[class='_42ft _4jy0 rfloat _ohf _4jy4 _517h _51sy']
or
a[rel='async-post']
Now all these selectors will only be useful if the attribute is unique. But take a look at that article there are many tricks you can use to make CSS selectors work for you.
By using xpath with contains text you can click on the element(below is the answer)
driver.findElement(By.xpath("//a[contains(text(),'Ileri')]")).click();
Try it and let me know if it works for you
Try any of these below mentioned code.
Using id locator
driver.findElement(By.id("u_9_8")).click();
Using xpath locator
driver.findElement(By.xpath("//a[text()= 'İleri']").click();
Explanation:- Use text method along with <a>tag.
driver.findElement(By.xpath("//a[#role='button'][text()= 'İleri']").click();
Explanation:- Use role attribute and text method along with <a> tag.
Please add the wait conditions before you are going to click
Element clicking Via linkText :
webDriver.findElement(By.linkText("İleri")).click();
Element clicking Via id :
webDriver.findElement(By.id("u_9_8")).click();
Element clicking Via cssSelector :
1.webDriver.findElement(By.cssSelector("._42ft._4jy0.rfloat._ohf._4jy4._517h._51sy")).click();
2.webDriver.findElement(By.cssSelector("a[class='_42ft _4jy0 rfloat _ohf _4jy4 _517h _51sy']")).click();
Element clicking Via javaScript :
((JavascriptExecutor) driver).executeScript("arguments[0].click();", webElement);
Here you need to pass the element locator instead of webElement.
We can see ID attribute tag so we can use ID "u_9_8" to click on the button.
use the below code.
driver.findelement(By.id("u_9_8")).click();
I think you should be able to use the id
driver.findElement(By.id("u_9_8")).click();
Give it a shot
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.
3 matching nodes.. all have same source code ..hence it is failing //img[(#src='/PHYLINSPortlet/images/override-0.gif')]
<img id="_PHYLINSPortlet_WAR_PHYLINSPortlet_INSTANCE_o3P5_:form_PolicyContent_UI2:Messages:0:j_id1885:0:j_id897" class="null" alt="" src="/PHYLINSPortlet/images/override-0.gif" style="border:0px"/>
This is the xpath of a dynamic button:
I need to click on all the buttons.
what I did is --
List<WebElement> buttons = driver.findElements(By.xpath("//img[(#src='/PHYLINSPortlet/images/override-0.gif')]"));
for( WebElement button : buttons ) {
button.click();
}
It is little bit difficult to provide the proper solution of your problem as you haven't shared your HTML.
As I observe the image in the question. You have 3 same types of image and want to click on any specific image lets say SECOND image then you need to use xpath methods like following-sibling or preceding-sibling to make your xpath unique
Example :-
Suppose you have any unique column in your table (Rule id) or you want to click a specific image.e.g. rule id Val01014 image
Use the methods like following xpath -
//img[#src='/PHYLINSPortlet/images/override-0.gif']/preceding-sibling::td[text()='Val_01014']
OR
//img[#src='/PHYLINSPortlet/images/override-0.gif']/following-sibling::td[text()='Val_01014']
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....
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')')]