I am new to Selenium which is probably why I am stuck.
Here is the screenshot showing inspect where two divs are almost identical but they both have another div inside containing different text values: Image.
Could I somehow choose the div I want by checking if getText() is equal to ...?
I found the following way but as far as I know, I would never know the order of the stream:
new ArrayList<>(chromeDriver.findElements(By.xpath("//div[#class='sign-up-row']"))).get(1).click();
Thank you guys.
Using Xpath like below:
example for //div[#class='sign-up-row'] this node
use text():
//div[#class='sign-up-row' and .//span[text()='Remember me.')]]
Or use with contains:
//span[contains(text(), 'Remember')]/ancestor::div[#class='sign-up-row']
Use the dot . can find inner element:
//div[#class='sign-up-row' and contains(., 'Remember')]
You can create an xpath using the text present in the tag within the div tag. This would return the specific element you are interested in.
Example for the xpath:
//div/div[text()='textforcomparison']
You can follow below approach.
List<WebElement> lst = driver.findElements(By.xpath("//div[#class='sign-up-row']"));
for (int i = 0; i < lst.size(); i++) {
if(lst.get(i).getText().equals("Text to Be Compared")) {
lst.get(i).click();
//If you want to break the foe loop after the match
break;
}
}
Related
I am trying to locate and click an element which is having same className as other elements. I am unable to differentiate that element from other to click that element. Here is the HTML code of that element:
<a href="/category/men/N-fh7rea" class="accord-header">
Men
</a>
In this code, classname is same as others elements and text "Men" is also same. So made an Xpath of this:
//a[#class='accord-header' AND contains(text(),'Men') ]
Tweak the xpath a bit and use :
//a[#class='accord-header' and #href='/category/men/N-fh7rea']
You can get more granular and use :
//a[#class='accord-header' and #href='/category/men/N-fh7rea' and contains(.,'Men')]
You can alos use :
//a[#class='accord-header' and #href='/category/men/N-fh7rea'][normalize-space()='Men']
if you can't find any difference you can always count whith one, from the same objects interest you. If its for example 3rd element, save all of them as a list using findElements, and then get third element from it.
List<WebElement> elems = driver.findElements(By.xpath("//a[#class='accord-header' and #href='/category/men/N-fh7rea' and contains(.,'Men')]"));
WebElement elementThatYouLookedFor = elems.get(2);
If You need to click all of elements of this kind just use foreach loop:
List<WebElement> elems = driver.findElements(By.xpath("//a[#class='accord-header' and #href='/category/men/N-fh7rea' and contains(.,'Men')]"));
for(WebElement we : elems){
we.click(); //or any other operation
}
In a web page , I see three h3 tags , and each h3 tag contains few links.
My scenario is : I want to go to one of the h3 tag and count the links only in that section and each of them.
Can anyone tell me how to click links of that specific section ?
You can do something like this:
List <WebElement> we = driver.findElements(By.xpath("//h3[text()='Quick Links ofHyderabad']/following::ul/a"));
int noOfLinks = we.size();
// To click on all the links just use a `for` loop
for(WebElement w : we) {
w.click();
}
Customize the code as per your need. It should be something like this:
//Pick all div elements:
List< IWebElement > elements = driver.FindElements(By.XPath(//div[contains(#class, 'description')]/h3));
// Now use a forloop to go through each element and grab the data you need.
String Data = elements.getText();
// Split the Data using regex and get the count as per your html code
I am creating an automatic test for some webs and I'm using WebDriver, TestNG and code that is written in Java. On the page is shown register of categories, in parentheses is number of auctions and i need to get this number as variable, but i don't know, how.
I use the following code:
WebElement number1_1_vse = (driver.findElement(By.linkText("Vše")));
String text_vse1_1 = number1_1_vse.getText();
but output is only: "Vše" without (number of auctions)
link to the website
screenshot -> image with the number
can anyone advise me please?
With linktext you are finding the nested a which text is Vše only. You need to find the li containing the text Vše (949)
Use the following css selector to identify the element
By bycss =By.cssSelector(".list.list-categories>li:first-child");
WebElement number1_1_vse = driver.findElement(bycss );
String text_vse1_1 = number1_1_vse.getText();
WebElement parent = number1_1_vse.findElement(By.xpath(".."));
// which will give you access to <li>
List<WebElement> childs = parent.findElements(By.xpath(".//*"));
// childs.get(1) has the result.
Please try to get the xpath value and then try to search it with below mentioned syntax :
findelementbyxpath("//*[#id="post-form"]/h2")
To get xpath value of and element : right click --> inspect element --> right click copy xpath
When trying to automate our application, there are two buttons with same name.
I'm not able to find a way to recognize these. Please let me know what could be the other ways to identify these elements in selenium webdriver in java
You can use xpath indexing option.
By.xpath("(//input[#name='Button'])[2]")
You can go with xpath always if there is no uniqueness with attribute. For e.g. if you want to find an element which has text foo and name button then I'll prefer xpath as below if name is not unique there:
//*[#name='button' and text()='foo']
Or For different class but same name
//button[#name='button' and #class='xyz']
or For different text but same name
//input[#name='button' and contains(text(),'Click Here')]
or for different tags but same name
//button[#name='button']
//input[#name='button']
Just go with any unique property and make a customized xpath.
I hope you can also use java script for this as well for e.g.
WebElement butttonToClick = driver.findElement(By.name("button"));
((JavascriptExecutor)driver).executeScript("arguments[1].click();",butttonToClick );
Where arguments[1] means second element which has same name.
You can go with xpath methods like following-sibling/preceding siblings.
For example if the Button is located to any unique webelement try to identify that webelement first and by using different xpath methods like following-siblings, content, preceding siblings you can access the web element.
Iterating loop on button with same name and same class
List<WebElement> listofItems=
driver.findElements(By.className("actions"));
System.out.println(listofItems);
System.out.println(listofItems.size());
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
for (int s=1; s<=listofItems.size(); s++)
{
/*Getting the list of items again so that when the page is
navigated back to, then the list of items will be refreshed
again */
listofItems= driver.findElements(By.className("actions"));
//Waiting for the element to be visible
//Used (s-1) because the list's item start with 0th index, like in
an array
wait.until(ExpectedConditions.visibilityOf(listofItems.get(s-1)));
//Clicking on the first element
listofItems.get(s-1).click();
Thread.sleep(2000);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.print(s + " element clicked\t--");
System.out.println("pass");
driver.navigate().back();
}
So for my project, I'm using Selenium to check that significant fields are in my DOM. The XML I am analyzing is below:
<results>
<result index="1">
<track>
<creator>God</creator>
</track>
</result>
<results>
For the first thing, I get a list of all result tags as webElements by running:
List<WebElement> result_list = driver.findElements(By.tagName("result"));
I then do a for loop in order to check that the creator tag is there by running
try {
for (int i = 0; i < result_list.size(); i++) {
WebElement track = result_list.get(i).findElement(By.tagName("track"));
System.out.println(track.findElement(By.tagName("creator")).getText());
System.out.println(track.getTagName());
System.out.println(track.getAttribute("creator"));
}
result = true;
}
catch (Exception e) {
result = false;
}
I have inserted print statements in order to see what each tag is saying. I'm new to selenium so I'm just trying to make sure that I'm iterating correctly for the web elements, meaning each call to getText and getAttribute should be different for each iteration of the loop. Only problem is that I get an empty string printed out for each getText() call and null for each getAttribute() call. Why is this happening? Output below.
<empty string> (nothing is printed, just illustrating the empty string)
track
null
Any help would be greatly appreciated!
Selenium doesn't handle XML. It's HTML only. Please read the documentation, it's quite clear.
There might be a plugin
WebElement.getAttribute(String) is not returning what you expect because there's a slight misunderstanding of what an attribute is. The attribute of an element is defined within the tag of the element ('id', 'name', 'style', etc.).
What you're trying to do is look for a child element of <track> when instead what you're doing is trying to look for <track creator="a_creator"> which would return what you're expecting.
I've never been able to get WebElement.findElement() to work on another WebElement without dropping down to xpath. So something like:
WebElement parent = driver.findElement(By.id("parent"));
WebElement child = parent.findElement(By.tagName("child"));
Never seems to work. However, in this case something like:
WebElement parent = driver.findElement(By.id("parent"));
WebElement child = parnet.findElement(By.xpath(".//creator"));
Should work. If you're new to xpath, you may want to consider a quick run through some basic tutorials.