Unable to click the elements in the dynamical list - selenium

For the below code
<li class="user ui-menu-item" role="presentation">
<a id="ui-id-52" class="ui-corner-all" tabindex="-1">
<em>User:</em>
Staff User
</a>
</li>
This is the scenario:
There is a text field, in which I enter the name Staff, with that the related values for the staff are displayed in the dynamic list box, in the above scenario the id is dynamically generated, and when I tried to select the value by class, it is same for all the elements.
I want an xpath expression to select the first available options in the list. I tried in many ways like with contains and starts-with, but no use. Please let me know your valuable suggestion.
Thanks in Advance
Shiva.

I think this should work
WebElement ele = webdriver.findElement(By.Xpath("//li[#class='user ui-menu-item'][1]"))

Did you try forming a List and then just directly using the first element of that List?
List<WebElement> list = driver.findElements(By.xpath("//li[#class(contains, 'user')]";
list[0].getText();
I'm still not 100% percentage sure what you want to do with the element once you have found it though. It would probably be better to form the List, then iterate over that list and perform whatever action you require per element.

Use this xpath
"(.//a[#class='ui-corner-all'])[1]"
source: XPath query to get nth instance of an element

Use the xpath. It should work
//li[#role = 'presentation']//a[1]

Related

Click on parent element based on two conditions in child elements Selenium Driver

Using Selenium 4.8 in .NET 6, I have the following html structure to parse.
<ul class="search-results">
<li>
<a href=//to somewhere>
<span class="book-desc">
<div class="book-title">some title</div>
<span class="book-author">some author</span>
</span>
</a>
</li>
</ul>
I need to find and click on the right li where the book-title matches my variable input (ideally ignore sentence case too) AND the book author also matches my variable input. So far I'm not getting that xpath syntax correct. I've tried different variations of something along these lines:
var matchingBooks = driver.FindElements(By.XPath($"//li[.//span[#class='book-author' and text()='{b.Authors}' and #class='book-title' and text()='{b.Title}']]"));
then I check if matchingBooks has a length before clicking on the first element. But matchingBooks is always coming back as 0.
class="book-author" belongs to span while class="book-title" belongs to div child element.
Also it cane be extra spaces additionally to the text, so it's better to use contains instead of exact equals validation.
So, instead of "//li[.//span[#class='book-author' and text()='{b.Authors}' and #class='book-title' and text()='{b.Title}']]" please try this:
"//li[.//span[#class='book-author' and(contains(text(),'{b.Authors}'))] and .//div[#class='book-title' and(contains(text(),'{b.Title}'))]]"
UPD
The following XPath should work. This is a example specific XPath I tried and it worked "//li[.//span[#class='book-author' and(contains(text(),'anima'))] and .//div[#class='book-title' and(contains(text(),'Coloring'))]]" for blood of the fold search input.
Also, I guess you should click on a element inside the li, not on the li itself. So, it's try to click the following element:
"//li[.//span[#class='book-author' and(contains(text(),'{b.Authors}'))] and .//div[#class='book-title' and(contains(text(),'{b.Title}'))]]//a"

Choose the correct element from the list of objects with the same className

Quick one, i am trying to avoid using xpath and using css selectors due to performance issues xpath can have so i would like to know the right approach of locating for example "A" in the list
<div class="input-search-suggests" xpath="1">
<div class="input-search-suggests-item">A</div>
<div class="input-search-suggests-item">B</div>
<div class="input-search-suggests-item">C</div>
</div>
Currently i am locating A using xpath / span but it would be indeed sufficient locating all elements and then grabbing A from the list that have same class which is "input-search-suggests-item"
#FindBy(xpath = "//span[contains(text(),'A')]")
CSS_SELECTOR does not have support for direct text what xpath has.
What this means is, for the below xpath
xpath = "//span[contains(text(),'A')]"
based on text A you can not write a css selector.
Instead to locate A using css selector, you can do :
div.input-search-suggests > div.input-search-suggests-item
In Selenium something like this :
#FindBy(cssSelector= "div.input-search-suggests > div.input-search-suggests-item")
Even though it will have 3 matching nodes, but findElement will take the first web element.
Also you may wanna look at nth-child(n)
div.input-search-suggests > nth-child(1)
to make use of index to locate A, B, C
Here is the Reference Link

Xpath finder for selenium using python -automation

I am trying to find an unique xpath for the below element, please advice if there are any better xpaths to make it for general text as I have currently given for that specific name.
<td tabindex="4" style="text-align: left;" title="name" class="">Name</td>
xpath i am using: //td[#title='name']
here if the name is changed with something else in the code, this wouldn't work, could someone help me identify unique xpath which works in general for any text. Thanks!
You can concatenate (using and / or) multiple attributes of element to find the element precisely .
By.xpath("//td[#title= 'name' and contains(text(), 'Name')]")
However we need to see more details of the code and your DOM of page to find element.
There will always be some element which will never change in the page(like name of table) using that as a relative point ,we can refer to the row of the table.
the simplest way to find the XPath of any element is to go to the developer options and select the markup of the element you want XPath of.
Right Click -> Copy -> XPath
I believe this is the simplest way. And you will also where you are doing wrong.
Screenshot attached for your reference.
I have used the general syntax - "//td[text()='{}']" and passing the name parameter when i define a method so that it won't be specific to one and others can test using the same locator with their name changed when someone else uses the testcase.
Thanks everyone for your response!

How can I get an element without id, name and class attributes in red box using By?

<div class="bInputTab">
<ul>
<li class="onNow">网银支付</li>
<li>账号支付</li>
</ul>
</div>
How can I get the element in the red box using By?
Many thanks!
Try following xpath,
//a[#onClick='On click Value']
You could use xpath, tagName, all depends on HTML structure, You can find parent element, and search downwards:
//li[#class='onNow']/following-sibling::li[1]/a
if its only link in DOM driver.findElement(By.tagName("a"));
Hope this helps,
This XPath should work :
//li[#class='onNow']/following-sibling::li[1]/a
The link text should also work as well
driver.FindElement(By.LinkText("账号支付"));
Actually I did not show the key structure of the HTML. It is because the element is not in the default frame. So I add the WDS.browser.switchTo().frame("frame_main") to the code, it works.
Thanks for all of your help.
The reference is The WebDriver Sampler: Your Top 10 Questions Answered
The element’s locator is invalid
2. The element belongs to another frame
The element is not available in DOM yet

how to click on element using WebDriver

i need to click on dat element:
<span id="act_action9" class="" onclick="openDialog('export', event)">
text
</span>
i cant click on id,because it is dynamic. After a click i am getting window with some settings.
You should use xpath in order to click this element, so you could add an expression that unequivocally identify this element.
Could you please include more HTML code, because just with the code included is not enough to create a xpath expression.
I recommend this tutorial to start doing good xpath expressions:
http://www.w3schools.com/xpath/
See example for dynamic xpath
By.xpath("//span[#id [contains(.,'act_action')]]")
Great xpath cheatsheets: http://extract-web-data.com/5-best-xpath-cheat-sheets-and-quick-references/
You can also use xpath like following(assuming only digit is dynamic):
".//span[contains(#id, 'act_action')]"
As ID is dynamic, you can use xpath for text which is inside span.
driver.findElement(By.xpath("//span[text()='text']").click();
Or if part of ID remain common, then you can use
//span[contains(#id,'act')]