Java Selenium - Select a WebElement based on value attribute - java

I was wondering if there was a way to find an element on a webpage based on the value attribute.
<option value="abc">Some text here</option>
<option value="bcd">Some text here</option>
I figured I could just create a list of WebElements based on the tag name and traverse each one using .getAttribute("value"), but I was wondering if there was a more effective way of doing this similar to the way you can find an element based on its text using:
driver.findElement(By.xpath("//*[contains(text(), '" + term + "')]"))

You can do this:
driver.findElement(By.cssSelector("[value=\"abc\"]"))
and you would change the value of value depending on what you were trying to find.

To locate the element with value="abc" on a webpage `based on the value attribute of abc you can use either of the following Locator Strategies:
css-selectors:
driver.findElement(By.cssSelector("option[value='abc']"))
xpath:
driver.findElement(By.xpath("//option[#value='abc']"))

Related

accessing the class name Selenium Webdriver

I am trying to access a web element in this example I am trying to access the className
driver.findElement(By.className("more-option")).click();
from bellow but it fails.
<div class="text-center">
<a class="small text-muted js-more-options" href="#">More
Options</a> = $0
</div>
My goal is to be able to test the ability to click More options button
Edit
I have tried
driver.findElement(By.cssSelector("td[title='More options']")).click();
and
driver.findElement(By.partialLinkText("options")).click();
There are to many option you can click on element.You can use contains().
Contains() is a method used in XPath expression.
driver.findElement(By.XPath("//a[contains(text(),'More Options')]")).Click();
or
driver.findElement(By.XPath("//a[contains(#class,'small')]")).Click();
if you get more than one element then you have to use index and click on particular element.
Find element using By.className just for single class name.
Try following approach.
By css selector:
driver.find_element_by_css_selector('div.text-center a.small.text-muted.js-more-options').click()
driver.find_element_by_css_selector('a[class="small text-muted js-more-options"]').click()
By xpath:
driver.find_element_by_xpath('//div[#class="text-center"]//a[#class="small text-muted js-more-options"]').click()
By partial link text:
driver.find_element_by_partial_link_text('Options').click()

How to find an element having random id attribute through Selenium

How do I locate the input element by id where the last 2 numerical digits change randomly?
<input id="start-time-hours-c31" class="focused" name="startTimeHours" value="" placeholder="hh" maxlength="2" type="text">
Quick answer
When last 2 numerical digits change randomly,
driver.findElement(By.xpath("//input[contains(#id,'start-time-hours-c')]"));
Using 2 attributes of Input tag,
driver.findElement(By.xpath("//input[#name='startTimeHours' and #class='focused']"));
Please let us know if it resolved your query.
As the last 2 characters of the id attribute changes randomly, additionally you need to consider some other attributes as well and you can use either of the following Locator Strategies:
cssSelector:
WebElement elem = driver.findElement(By.cssSelector("input[id^='start-time-hours-c'][name='startTimeHours']"));
xpath:
WebElement elem = driver.findElement(By.xpath("//input[starts-with(#id, 'start-time-hours-c') and #name='startTimeHours']"));
As the html also has a name tag you can find the element by using that name tag, like:
WebElement element = driver.findElement(By.name("startTimeHours"));
And if the name tag startTimeHours is not unique on the page, then you can find the element by using the below xpath:
WebElement element = driver.findElement(By.xpath("//input[contains(#id,'start-time-hours')]"));
Agreed with #TheSociety answer, you can also be used as the combination of name and id
//input[contains(#id,'start-time-hours-c') and contains(#name,'startTimeHours')]
Use contains, sibling or Ancestor when the value is dynamic.
xpath - //input[contsins(#id,'start-time-hours-c')]
You can use either of them:
//input[starts-with(#id,'start-time-hours-c')]
(or)
//input[contains(#id,'start-time-hours-c')]
if the above returns a single webelement then you can use it or else if it returns multiple elements go for name attribute in the xpath.

Locating label element using class

I'm trying to locate the label element and fill it with some value but I'm not able to get it. I'm using Java, testNG and Selenium to write the below code.
The code that I used is below
driver.findElement(By.className("ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-maxlength ng-touched")).sendKeys("12345");
OR
driver.findElement(By.className("item-input-wrapper scan-input-label")).sendKeys("12345");
This is the details of element that I received when I inspect the element.
Actually selenium doesn't support to locate an element using By.className() with compound class name. You should try using By.cssSelector() instead to locate <input> element as below :-
driver.findElement(By.cssSelector("input[placeholder = 'Scan Container Label']")).sendKeys("12345");
Or more specific :-
driver.findElement(By.cssSelector("input[placeholder = 'Scan Container Label'][ng-model *= 'ctrl.currentValue']")).sendKeys("12345");
Edited :- If you want to locate <label> element using their class name try as below :-
driver.findElement(By.cssSelector("label.item-input-wrapper.scan-input-label"));
Or you can locate it also using By.className() with anyone if the single class name as :-
driver.findElement(By.className("item-input-wrapper"));
Actually selenium doesn't support to locate an element using By.className()
You can try syntax of css with classname, css=tagname.classname
i.e.
driver.findElement(By.cssSelector(input.item-input-wrapper)).sendKeys("12345");

Selenium WebDriver Java locating an element with dynamic ID

I'm currently having troubles on locating this element with dynamic id. Here are the screenshots below.
What i have right now is the element of Variables (8) //a[contains(.,'Variables (8)')]. What i need is only the "Variables" since the number 8 is always changing.
Any thoughts on this? Any ideas will be much appreciated. Thanks
First of all 'Variables (8)' is not Id, its text. Id's are not dynamic since they represent unique identifier for the web element. This will look like this (based on your example):
<div class="field" id="fieldId">
As for your question, you can find the element by partial linked text:
driver.findElement(By.partialLinkText("Variables"));
This will give you the a element no meter what the number is.
You can try the below:
driver.findElement(By.cssSelector("a:contains('Variables')"));
If you want the word "Variables" , use the below:
String str = driver.findElement(By.cssSelector("a:contains('Variables')")).getText().split(" ")[0];
Hope this Helps....
What I understand from your question is you want to locate the <a> tag which contains text "Variables".
Try using this xpath:
//div[#class="field"]/a[contains(.,"Variables")]
This xpath will locate the <a> tag after the div tag with class name =field and Contains method with <a> tag will find the element which contains text "Variables"
try this:
String varText = driver.findElement(By.cssSelector("div.triggerFirst>div:nth-child(1)>a")).getText();

Selenium WebDriver Finding Element by Partial Class Name

In the frame I'm working with, I have the following element:
<div class="x-grid3-cell-inner x-grid3-col-expRepCol"> New session from client IP 192.168.5.3 (ST=/CC=/C=) at VIP 192.168.5.2 Listener /Common/Tomcat (Reputation=Unknown)</div>
As well as many other similar elements. I am trying to locate this element by partial name text and click it with the following code:
String expectedText = "New session from client IP";
driver.findElement(By.className("div[class*='"+expectedText+"']")).click();
And I have also tried with cssSelector:
String expectedText = "New session from client IP";
driver.findElement(By.cssSelector("div[class*='"+expectedText+"']")).click();
But WebDriver keeps throwing an exception stating it's unable to locate that element. Any suggestions as to what could be the problem?
<div class="dd algo algo-sr Sr" data-937="5d1abd07c5a33">
<div class="dd algo algo-sr fst Sr" data-0ab="5d1abd837d907">
Above 2 are the HTML elements in yahoo search results. So if we wanna get these elements using partial class name with selenium python, here is the solution.
driver.find_element_by_css_selector("div[class^='dd algo algo-sr']")
In the same way we can get any elements with partial match on any attribute values like class name, id etc.
find elements with css selector partial match on attribute values
By.className is looking for a class with the name entered.
By.cssSelector is looking for a match for the selector you entered.
What you're attempting is to match the text of the div against class, which won't work.
You can try something like this:
driver.findElement(By.xpath("//div[contains(text(),'"+expectedText+"')]")).click();
I believe this will work:
driver.findElement(By.className("x-grid3-cell-inner x-grid3-col-expRepCol").click();

Categories

Resources