How to inspect any element in selenium - java

I am trying to perform the mouse hover, where 2 elements have same class name. I want to click on second element but its clicking on first element. How to locate the second one if there is only class and href attributes given.
I've tried to create my own xpath using classname and href but its not working.

You can try using the className selector and then using index to get the "2nd" element with that class name.
For this element:
<a class="desktop-main" data-reactid="137" style="border-bottom-color:#fb56c1;" data-type="navElements" data-color="#fb56c1" data-group="women" data-index="1" href="/shop/women">Women</a>
Try this:
List<WebElement> elements = driver.findElements(By.className("desktop-main");
elements.get(1).click();

Related

Can not find button. Selenium java

I have this code:
Then using selenium I write this:
driver.findElement(By.className("extra-delivery-item")).click();
The question is why does selenium tell me:
no such element: Unable to locate element: {"method":"css selector","selector":".extra-delivery-item"
P.S. I have already located some buttons and successfully clicked on them, everything works, except that button.
This element is actually has multiple class names. So to select it according to single class name you can use XPath or CSS selector.
Like this:
driver.findElement(By.xpath("//div[contains(#class,'extra-delivery-item')]")).click();
or
driver.findElement(By.cssSelector("div.extra-delivery-item")).click();
Selecting element by class name, like you tried to use
findElement(By.className("extra-delivery-item"))
means matching element with class attribute exactly matching that value, extra-delivery-item in this case. While the element class attribute here is extra-delivery-item extra-delivery-item-selected

How do i click this element via className since i will be using it for multiple screens with same class name

The element I got is <div class="x-tool x-box-item" id="tool-123" src="data:image/gif;base64" class="new-img x-tool-maximize".
I particularly need this class="new-img x-tool-maximize" because its the common of all the screen.
I already tried
driver.findElement(By.className("new-img.x-tool-maximize")).click()
and
driver.findElement(By.className("new-img x-tool-maximize")).click();
and
driver.findElement(By.xpath("//div[contains(#class, 'value') and contains(#class, 'test')]"))``;
You should be able to use a CSS selector to find this type of element. You’ll want something like driver.findElement(By.cssSelector("div.new-img.x-tool-maximize")).
You can use list in selenium if you have multiple elements with same locators
List<WebElement> classes=driver.findElements(By.classname("new-img x-tool-maximize));
// if you want click 1 st class name element use this following line
classes.get(0).click();
OR // if you want click 2 nd class name element use this following line
classes.get(1).click();
Use a list to get all the WebElements with specific classes
List<WebElement> list = driver.findElements(By.cssSelector("div.new-img.x-tool-maximize"));
As you intent to click() on the element using only the className attribute values, ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector using only the className attribute values:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector(".new-img.x-tool-maximize"))).click();
For a more canonicl approach you can indlude the tagName as follows:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.new-img.x-tool-maximize]"))).click();
References
You can find a couple of relevant detailed discussions in:
Unable to locate element using className in Selenium and Java
How to locate the last web element using classname attribute through Selenium and Python
What are properties of find_element_by_class_name in selenium python?

Clicking an element having same class name as others

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
}

How to retrieve value from span in class

I want to click on hospital panel. The contains a class attribute and inner text and i need to click on "后三".
I get an error saying "unable to locate element". What am I doing wrong?
HTML:
<span class="tab-back" value="2" tag="0" default="0"><span class="tabbar-left"></span><span class="content">后三</span><span class="tabbar-right"></span></span>
CODE :
element = driver.findElement(By.xpath("//span[#class='content']/text()[last()]"));
it's seems not getting the value of 后三. Kindly advise
Use the xpath below:
WebElement element = driver.findElement(By.xpath("//span[#class='content' and contains(text(),'后三')]"));
And if multiple elements there, you can use:
List<WebElement> elements = driver.findElements(By.xpath("//span[#class='content' and contains(text(),'后三')]"));
The desired item you can access by for example:
elements.get(index)
Where index is your desired index like 0,1 etc.

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

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();
}

Categories

Resources