How to Locate an Element Where Multiple Elements Have Same Id, Name and Attributes in Same Tags Through Selenium and Java

How to access the second element that has the same class name in selenium using java

You can use xpath indexing option.

By.xpath("(//input[@name='Button'])[2]")

How to locate an element which have same name and same atrributes in different tags in selenium

Use it like this:

By.xpath((.//*[@id='123'])[2])

the xpath should be in braces. The [2] at the end denotes the index. i.e. [1] will be the first div tag, [2] will be the second div tag.

I have encountered this issue lot of times. This happens in many websites.

Selenium - How to get attribute values with same tag names and no class/id?

Instead of findElement() you can use the findElements() method to create a List and iterate through the List to print the values of the attribute content as follows :

List <WebElement> urlTag = driver.findElements(By.xpath("//meta[@property='article:tag']"));
for(WebElement tag:urlTag)
System.out.println(tag.getAttribute("content"));

How to use multiple locators to find an element in selenium webdriver

Xpath allows you to use and and or to evalute multiple attributes.
so you can form an xpath using this

//input[@id='id' and @value='value1' or @value='value2']

For example on google home page, there are two buttons, Google Search and
I'm Feeling Lucky. Both has same type submit to find these buttons I can form an xpath similar to this

//input[@type='submit' and @value='Google Search' or @value="I'm Feeling Lucky"]

Get the different value from multiple elements with the same class in Selenium for Python?

Use find_elements_by_class_name. It will return a list of all elements with this class name.

findclass = driver.find_elements_by_class_name("class_name")

for i in findclass:

print(i.text)

To find the second element in the list just use: print(findclass[1].text).

If you are looking for a specific value you can use CSS selector or XPath, you don't need the list of all the elements...

How to find an element having random id attribute through Selenium

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.

Get the text from multiple elements with the same class in Selenium for Python?

find_element_by_xpath returns one element, which has text attribute.

find_elements_by_xpath() returns all matching elements, which is a list, so you need to loop through and get text attribute for each of the element.

all_spans = driver.find_elements_by_xpath("//span[@class='class']")
for span in all_spans:
print span.text

Please refer to Selenium Python API docs here for more details about find_elements_by_xpath(xpath).



Related Topics



Leave a reply



Submit