I have a list of elements:
<ul>
<li></li>
<li></li>
<li style="display: none"></li>
<li style="display: none"></li>
<li style="display: none"></li>
<li></li>
</ul>
I want to take only elements without this style="display: none".
Any suggestions how to distinguish between those with style="display: none" and those without?
List<WebElement> lis = webDriver.findElement(By.cssSelector("ul li:not([style*='display: none'])"));
List without display : none element...
This allows you to get all <li> elements that don't have style="display: none" in their HTML code :
driver.findElement(By.cssSelector('li:not([style="display: none"])'));
How about doing ,
String s = webelement.getAttribute("outerHTML");
Now use the String s to parse the style attribute.
You can use cssSelector with :not
driver.findElements(By.cssSelector("li:not([style='display: none']"));
This will get all <li> tags that doesn't have style='display: none'
Related
I try to click on the select menu and to choose the element:
<div id="_desktop_currency_selector">
<div class="currency-selector dropdown js-dropdown">
<span>Currency:</span>
<span class="expand-more _gray-darker hidden-sm-down" data-toggle="dropdown" aria-expanded="false">UAH ₴</span>
<a data-target="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="hidden-sm-down">
<i class="material-icons expand-more"></i>
</a>
<ul class="dropdown-menu hidden-sm-down" aria-labelledby="dLabel" style="display: none;">
<li>
<a title="EUR" rel="nofollow" href="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&id_currency=2" class="dropdown-item">EUR €</a>
</li>
<li class="current">
<a title="UAH" rel="nofollow" href="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&id_currency=1" class="dropdown-item">UAH ₴</a>
</li>
<li>
<a title="USD" rel="nofollow" href="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&id_currency=3" class="dropdown-item">USD $</a>
</li>
</ul>
<select class="link hidden-md-up">
<option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&id_currency=2">EUR €</option>
<option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&id_currency=1" selected="selected">UAH ₴</option>
<option value="http://prestashop-automation.qatestlab.com.ua/ru/?SubmitCurrency=1&id_currency=3">USD $</option>
</select>
</div>
</div>
My way:
WebElement element1 = driver.findElement(By.className("link hidden-md-up"));
Select dropList = new Select(element1);
// debug sysout
dropList.getOptions().forEach(p -> System.out.println(p.getText()));
In result I get this exception:
org.openqa.selenium.InvalidSelectorException: invalid selector:
Compound class names not permitted
How to correctly click on an element, using selenium?
It will not work with select class because, its not pure drop-down, its un-ordered lists of currencies
you need to open drop down by clicking on it, do something like this
driver.driver.findElement(By.xpath(".//div[#class='currency-selector dropdown js-dropdown']/a[#data-toggle='dropdown']")).click();
with this drop down list will open, now get individual element from list using xpath-
.//a[#title='USD'] or .//a[#title='USD']/parent::li
The exception is due to the multiple classes used in the selector. Change your selector to use single class or a cssSelector. See example below.
Check if these selectors return unique(required) element.
WebElement element1 = driver.findElement(By.className("hidden-md-up"));
OR
WebElement element1 = driver.findElement(By.cssSelector(".link.hidden-md-up"));
My list has approximate 190 items and items also getting traced one by one in for loop. I applied the condition to a specific text. however, debugger entering into condition but selecting always the first element from the drop-down. In mentioned code country "America" present on the 4th index of my drop-down.
List<WebElement> options=driver.findElements(By.xpath("//*[#id='Countryitems_popup']/div[1]/ul//li/span"));
for(int i=0;i<options.size();i++)
{
WebElement select=options.get(i);
String innerhtml=select.getAttribute("innerHTML");
if(innerhtml.contentEquals("America"))
{
select.click();
break;
}
}
HTML is mentioned below:
<div id="Countryitems_popup" class="e-scroll e-js e-wid" tabindex="" style="height: auto; display: block;">
<div class="h-con" style="height: 150px; width: 158.891px;">
<ul class="H-Kl" role="listbox">
<li data-value="001" id="004" role="option" unselectable="on" class="">
<span class=" e-ddltxt">Country 1</span>
</li>
<li data-value="676" id="006" role="option" unselectable="on" class="">
<span class=" e-ddltxt">Country 2</span>
</li>
<li data-value="765" id="009" role="option" unselectable="on" class="">
<span class=" e-ddltxt">Country 3</span>
</li>
<li data-value="0067" id="065" role="option" unselectable="on" class="">
<span class=" e-ddltxt">America</span>
</li>
</ul>
</div>
</div>
Use for each loop for more readability. Try with this code :
List<WebElement> options= driver.findElements(By.xpath("//[#id='Countryitems_popup']/div[1]/ul//li/span"));
for(WebElement option : options) {
if(option.getText().trim().equals("America")) {
option.click();
break;
}
}
This should work, provided the xpath should be correct.
Without seeing the page to test it, it's hard to know exactly what the issue is but if it's like other pages I've seen you will need to:
Click the "dropdown" to open it (and make the options visible)
Wait for the desired option to be clickable (indicating the list is open and visible)
Click the desired option
My recommendation is to put this all in a method called selectCountry() (or something similar) and pass in the desired country name.
public void selectCountry(String countryName)
{
// I'm assuming this is the container for the dropdown. Clicking it should open the dropdown
driver.findElement(By.id("Countryitems_popup")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='Countryitems_popup']//span[.='" + countryName + "']))).click();
}
You can achieve it with selector itself without loop. The xpath to find the list element with text 'America' will be as,
driver.findElement(By.xpath("//[#id='Countryitems_popup']/div[1]/ul//li[conatins(text(), 'America')]")).click()
I am using Selenium WebDriver with Java.
Below is my html code:
<div id="servicetype-pp" class="z-combobox-popup " style="display: none; width: auto;">
<ul id="servicetype-cave" class="z-combobox-content">
<li id="zk_comp_140" class="z-comboitem">
<li id="zk_comp_141" class="z-comboitem">
<span class="z-comboitem-image"></span>
<span class="z-comboitem-text">Bill Generation Service</span>
</li>
<li id="zk_comp_142" class="z-comboitem">
<li id="zk_comp_143" class="z-comboitem">
<li id="zk_comp_144" class="z-comboitem">
<li id="zk_comp_145" class="z-comboitem">
<li id="zk_comp_146" class="z-comboitem">
<li id="zk_comp_147" class="z-comboitem">
<li id="zk_comp_148" class="z-comboitem">
<li id="zk_comp_149" class="z-comboitem">
<li id="zk_comp_150" class="z-comboitem">
</ul>
</div>
I have defined a WebElement by xpath containing a particular text.
//div[#id='servicetype-pp']//span[contains(text(),'Bill Generation Service')]
It is not working. But, when I search with a single word, without any space, it is working fine.
//div[#id='servicetype-pp']//span[contains(text(),'Bill')] or
//div[#id='servicetype-pp']//span[contains(text(),'Generation')] or
//div[#id='servicetype-pp']//span[contains(text(),'Service')]
It seems it is an issue with space.
Please help.
TIA.
You can try using normalize-space()
//div[#id='servicetype-pp']//span[contains(text()[normalize-space()], 'Bill Generation Service')]
Actually your <div id="servicetype-pp" class="z-combobox-popup " style="display: none; width: auto;"> looks as invisible that's why you are not able to interact with the element, try as below :-
WebElement el = driver.findElement(By.id("servicetype-pp"));
el = (WebElement)((JavascriptExecutor)driver).executeScript("arguments[0].style.display = 'block';return arguments[0]", el);
//Now you can find your desire element
WebElement spanElement = el.findElement(".//span[contains(text(),'Bill Generation Service')]");
//Now do your further steps with this element
Edited :- If you are getting NuSuchElementExcpetion that means visibility is not the issue, It might be possible that this element is inside a frame or iframe. If it is, you need to switch that frame or iframe before finding element as below :
driver.switchTo().frame("frame name or id");
// Now go to find element
WebElement spanElement = driver.findElement(".//span[contains(text(),'Bill Generation Service')]");
Please see the code below where I need to click on dropdown so that list is displayed
<span class="k-widget k-dropdown k-header" style="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="" aria-disabled="false" aria-readonly="false" aria-busy="false">
<span class="k-dropdown-wrap k-state-default k-state-hover k-state-focused" unselectable="on">
<span class="k-input" unselectable="on">Is equal to</span>
<span class="k-select" unselectable="on">
<span class="k-icon k-i-arrow-s" unselectable="on">select</span>
</span>
</span>
driver.FindElement(By.CssSelector(".k-widget k-dropdown k-header")).click()
I think you again have to try by clicking using JavascriptExecutor
WebElement element1 = driver.findElement(By.xpath("//span[#class='k-widget k-dropdown k-header' and #role='listbox']"));
JavascriptExecutor executor1 = (JavascriptExecutor) oBrowser;
executor1.executeScript("arguments[0].click();", element1);
If above Xpath didn't work then try below:-
//span[#class='k-widget k-dropdown k-header']
Hope it will help you :)
I ran across this exact issue the other day. The problem is the Kendo UI <span> element you are referencing uses a <ul> element to get it's innertext but it's the <ul> list that handles the option change/selection. You won't be able to select an option from the pop-up/dropdown you see when clicking the <span>, you have to click the <li> element that holds the text options referenced by the span.
In your example, somewhere else in the HTML document you'll find a <ul> list similar to...
<ul unselectable="on" class="k-list k-reset" tabindex="-1" aria-hidden="true" aria-live="off" data-role="staticlist" role="listbox">
<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" data-offset-index="0">is equal to</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item" data-offset-index="1">another option</li>
</ul>
Using Selenium 3, if you wanted to select "another option":
driver.findElement(By.xpath("//li[text()='another option']")).click();
You should see your <span> innertext change to 'another option' and you can continue on your testing journey.
I want to extract the values from the "From" drop down of http://www.makemytrip.com/flights/ and then compare it with a particular search string to find if the string exist in the values from the drop down. But I am unable to extract the values as these fields are hidden. It is an autocomplete dropdown.
The html part of the drop down
<div class="container">
<div class="search_selector" style="display: none;">
<div class="col-xs-12">
<div class="city_sel_top">
<div class="top_cities_scroll" style="margin-top: -5px;">
<ul id="near_button" class="top_cities_list" style="width: 98%;">
<ul class="from_city_list top_cities_list" style="width: 98%; display: none;">
<li class="top_cities_heading text-right">Top Domestic Cities</li>
<li id="DEL|Y|New Delhi|India">
<a class="from_city_value1" href="javascript:void(0);">New Delhi, India (DEL)</a>
</li>
<li id="DEL|Y|New Delhi|India">
<a class="from_city_value1" href="javascript:void(0);">New Delhi, India (DEL)</a>
</li>
<li id="BOM|Y|Mumbai|India">
<a class="from_city_value2" href="javascript:void(0);">Mumbai, India (BOM)</a>
</li>
<li id="BLR|Y|Bangalore|India">
<a class="from_city_value3" href="javascript:void(0);">Bangalore, India (BLR)</a>
</li
>
......
I tried :
WebDriver driver;
List<WebElement> li = driver.findElements( By.xpath("//div/div[2]/ul[2]/li/a"));
This gives me a no such element exception as the first li element under ul[2] does not have a 'a' element under it. I want to get the 'a' element starting from the second li element.
Your XPath is too strict! Use By.tagName("a").
If you only want 'a' of the first item, then I think SiKing's solution is the best. But it you have to use xpath, then I think it should be the following:
//div/div/div/div[2]/ul[2]/li[2]/a