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 answerWhen 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
How to Pass JavaScript Values to Scriptlet in Jsp
How a Jar File Can Read an External Properties File
How to Remove Padding Around Buttons in Android
How to Do a Limit Query in Jpql or Hql
Set Zoom Level to 100% in Selenium Webdriver When Default Is Not 100
Java.Lang.Illegalargumentexception: No Converter Found for Return Value of Type
Lombok Is Not Generating Getter and Setter
Can We Use Multipart and @Requestbody Together in Spring
Regex to Validate 3 Repeating Characters
Forwarding the Request from One Jsp to Another Jsp With All Request Parameters
How to Join Results of Multiple Tables in Spring JPA Repository
Save Child Objects Automatically Using JPA Hibernate
How to Check Json in Response Body With Mockmvc
Field Required a Bean Which Could Not Be Found in Springboot
Spring Boot - Post Method Not Allowed, But Get Works
How to Change Read-Only Status of Files in Intellij
What Is the Most Elegant Way to Check If All Values in a Boolean Array Are True
How to Make Sonar Ignore Some Classes for Codecoverage Metric