Selenium: Getting contents of drop-down list without expanding it [duplicate] - selenium

This question already has answers here:
How to read text from hidden element with Selenium WebDriver?
(8 answers)
Closed 3 years ago.
I'm attempting to get a list of elements in a dropdown list on a page wihtout expanding the list first.
I've got a piece of html, where a dropdown list is created like this:
<est-select id="egenkapitalType0" name="egenkapitalType0" value="Velg type egenkapital…" <div class="select">
<input type="text" class="select-trigger id="egenkapitalType0-input" placeholder="Velg type egenkapital…">
<ul role="listbox" id="egenkapitalType0-liste" class="select-liste" aria-hidden="false" ng-transclude="">
<li id="egenkapitalType0-0" role="option" class="select-valg data-verdi="BANKINNSKUDD_BSU" value="BANKINNSKUDD_BSU">Bankinnskudd/BSU</li>
<li id="egenkapitalType0-1" role="option" class="select-valg data-verdi="AKSJER" value="AKSJER">Aksjer</li>
</ul>
(I've removed lots of irrelevant html code, so this sample might lack some closing etc.)
Is this possible? Either the displayed text or the values? Or is the access to the elements dependant on them being visible and accessible on the page?

To get the text using getText() the options need to be visible. You can use getAttribute("innerHTML") or getAttribute("textContent") on hidden elements.

You can access elements and it;s not dependent on visibility.
List<WebElement> elms = driver.findElements(By.xpath("//ul[#role='listbox']//li");
Now you have list of web elements and you can, get text, for example.
List<String> textFromElements = elms.stream().map(el -> el.getText()).collect(Collectors.toList());

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"

How to identify an element whose id is dynamically changing and has no other attributes in selenium?

I've this html body for a kendo dropdown list which has only one attribute i.e. id which is dynamically changing, how do i identify this object on every page refresh accurately.
other attributes like class and tab index already are present with same values multiple times on the same page for other dropdowns-
<span role="listbox" unselectable="on" class="k-dropdown-wrap k-state-default" id="dde13a91-2bf3-4e41-af72-bee1b881a8d9" dir="ltr" readonly="false" tabindex="0" aria-disabled="false" aria-readonly="false" aria-haspopup="true" aria-expanded="false" aria-owns="48f666d8-4c3c-43a8-a4dc-8e7a9961a0ef" aria-activedescendant="ca3c4431-3ebf-46c0-9510-a64a32eae108-C.US.0000110896">
<span unselectable="on" class="k-input">
<!---->
<!---->2018 ALBERTSONS / Beverage Mixes
</span>
<span unselectable="on" class="k-select">
<span class="k-i-arrow-s k-icon"></span>
</span>
<!---->
</span>
If there is a always present value in dropdown, You can try to find this value by text or something, and then get a parent:
XPath: Get parent node from child node
Other way is to get it by xpath of structure, like: div[3]/.../span etc. It is not good as every change can fail Your test, but if You have no other options, then You might want to try this.
To click on the element with the only attribute-text as 2018 ALBERTSONS / Beverage Mixes you need to induce WebDriverWait for the element to be clickable and you can use the following solution:
(Java) xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='k-dropdown-wrap k-state-default' and #role='listbox']//span[#class='k-input']"))).click();
I found a workaround to locate the element:
First get the xpath of the span tag inside the dropdown using it's text as below:
//text()[contains(.,'2018 ')]
since '2018 ' is common irrespective of adjacent text
Then move up to the parent tag of the dropdown to locate the dropdown frame which can be collapsed on click:
//text()[contains(.,'2018 ')]/parent::/parent::
then simply click on the element which is located.
driver.findElement(By.xpath("//text()[contains(.,'2018 ')]/parent::/parent::")).click();

Artificial Intelligence in UI Automation testing [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am currently working on a project where the xpath of the elements changes continuously. At the UI display level there is not much visible change in UI, however the DOM elements changes continuously.
I am thinking of a better approach to handle failures in test case due to minor changes in DOM structure. I am currently using Selenium with TestNg framework for UI Automation Testing.
Any suggestions or directions on alternate approach would be helpful.
Are you following the orders of locators suggested by Selenium contributors: if not then please follow this order :
ID
name
className
linkText
partialLinkText
tagName
cssSelector
XPath.
Note : Most of the time a cssSelector can replace Xpath, However Xpath has its own advantages which cssSelector do not provide.
For more reference you can go through this SO Link : Css vs Xpath
My suggestion would be:
1. Try using different locators for particular element[https://api.jquery.com/multiple-selector/]
something like selector1, selector2, selectorN. If selector1 is not available in DOM, control will not throw an error instead, it will search selector2 and so on
2. Use Explicit waits
I use By.CSS selectors rather than by xpath, as these are less prone to changes in the DOM.
So for this example dom:
<div class="smc-login-container">
<form role="form" action="/login.html" method="POST" name="login" class="ng-pristine ng-valid">
<!-- Username -->
<label for="username">User ID:</label>
<input type="text" class="smc-login-control aftLoginUsernameField" id="username" name="username">
<!-- Password -->
<label for="password">Password:</label>
<input type="password" class="smc-login-control aftLoginPasswordField" id="password" name="password">
<!-- Cross site scripting token -->
<input type="hidden" id="_csrf" name="_csrf" value="efaa05c4-77a8-443d-9484-51e8c9795c28">
<!-- Sign In Button -->
<button id="signIn" class="btn btn-lg btn-block smc-login-button aftLoginSignInButton">
Sign In
</button>
</form>
These selectors work:
Example 1. Find Button, element type and class
WebElement element = webDriver.findElement(By.cssSelector("button.btn"));
Example 2. Find Button, direct child rather than descendent
Uses full DOM and thus form element:
WebElement element = webDriver.findElement(By.cssSelector("div > form > button"));
Doesn't use form element:
WebElement element = webDriver.findElement(By.cssSelector("div button"));
Example 3. Find Input following the username label, attribute with value then it’s sibling (+)
WebElement element = webDriver.findElement(By.cssSelector("label[for='username'] + input"));
Example 4. Find Password input and Button, OR’d (comma) returning more than one element
List<WebElement> elements;
elements = webDriver.findElements(By.cssSelector("input.smc-login-control.aftLoginPasswordField , .btn"));
elements.get(0).sendKeys("my password");
elements.get(1).click();
Example 5. Find username input, has class attribute, but not attribute name = ‘password’:
WebElement element = webDriver.findElements(By.cssSelector("input[class]:not([name='password'])"));
Another thing I do is use a more generic pattern, which finds multiple items. I've found the items are always found in the same sequence and thus I can access the one I want by just using the array index (like in example 4).
Finally, if the DOM has dynamic values I've found that I can use a specific stable pattern to find a parent element which has the same dynamic value, thus extract it and then reuse it to find the other element I actually want.

Splinter Is it possible to use browser.fill without name as a query

I would like to use an absolute xpath to fill in a search bar. The ids and classes are dynamically generated and there is no name variable or instance. So it feels like I'm stuck without a tool to fill in boxes without the named variable.
Is there a way around this? Can I somehow change the absolute xpath to look like its a name assignment and then query and fill based on the new 'type' I assigned the absolute xpath?
Is there a method for this in Selenium if not available in Splinter?
I've select by CSS and I'm finding this error 'selenium.common.exceptions.InvalidElementStateException: Message: Element is not currently interactable and may not be manipulated'
Edit:
<div class="type-ahead-input-container" role="search">
<div class="type-ahead-input-wrapper">
<div class="type-ahead-input">
<label class="visually-hidden" for="a11y-ember10393">
Search
</label>
<!---->
<input id="a11y-ember10393" class="ember-text-field ember-view" aria-
autocomplete="list" autocomplete="off" spellcheck="false"
placeholder="Search" autocorrect="off" autocapitalize="off" role="combobox"
aria-expanded="true" aria-owns="ember11064" data-artdeco-is-focused="true"/>
<div class="type-ahead-input-icons">
<!---->
</div>
</div>
<!---->
</div>
</div>
As you have asked Is there a method for this in Selenium, the Answer is Yes.
Selenium supports Sikuli. Sikuli automates anything you see on the screen. It uses image recognition to identify and control GUI components. It is useful when there is no easy access to a GUI's internal or source code.
You can find more about Sikuli here.
Let me know if this answers your question.
When you get an error-message like that, it could be that your search result is not what you expected. It could be that you are getting more than one result, ie a list. On a list you can not input.
You can find the input boxes with an xpath, select the prefered one from the list (by trying) and put a text in it with the WebDriverElement _set_value method. That is not appropriate because of the _, but it is usefull.
input_list = browser.find_by_xpath('//div[#class="type-ahead-input"]/input[#class="ember-textfield ember-view"]')
input_list[1]._set_value('just a text to see whether it is working')

Get xpath under particular tag

Am newbie to selenium (java).
I have to click on the menu item which is under <ul> tag as <li> items. I can able to get it with //*[#id='MainMenu']/li[5]/span xpath.
I do not want to hard code [5] of the list item, because item's position may change. It may not at 5th position all the time.
I wanted to get xpath for the particular item under particular tag with an id.
Edit:
This is how my html looks like. List item text will be loading dynamically.
<ul id="sfMainMenu" class="sf-menu ui-selectable">
<li class="ui-selected ui-selectee">
<span subnav="0" param="cmd=desk" filesrc="/Dashboard/Index"></span>
</li>
<li class="ui-selectee"></li>
<li class="ui-selectee"></li>
<li class="ui-selectee"></li>
<li class="ui-selectee">
<span subnav="18" param="cmd=desk" filesrc="../myFile.aspx"></span>
</li>
</ul>
Kindly suggest the approach with an example.
I suggest trying this:
//*[#id='MainMenu']/li[normalize-space() = 'The text you want']/span
Though if you could show us what the HTML in question actually looks like, we can provide a more reliable answer. XPath doesn't have any concept of "visual text", so if you have some text that's hidden within the li you're trying to retrieve, that could be considerably trickier.
I have solved by using filesrc attibute of span tag in the list item.
Xpath is:
//span[contains(#filesrc, 'myFile.aspx')]
As my .aspx file will be the same for any page, so I used filesrc attribute which contains the actual file name and is the only file name in that html page.
You can use the following
driver.FindElement(By.XPath("//li[contains(Text(),'Expected Text']")).Click();
Or you can avoid xpath all together by running a foreach loop on the relevant Class tag
foreach (IWebElement e in driver.FindElements(By.ClassName("ui-selectee")))
{
if (e.Displayed && e.Text == "Expected Text")
e.Click();
}