I'm trying to find out the xpath for resource manager element, when I tried with this code,
driver.findElement(By.xpath("Resource Manager xpath")).click(), I turned up with error saying unable to find the element.
<div class="os-titlebar navbar navbar-inverse">
<div class="navbar-inner">
<div class="container">
<ul class="nav menu-primary os-navbar-icon">
<ul class="nav context-menu" style="">
<ul class="nav os-navbar-icon os-titlebar-shortcuts pull-right os_shortcuts">
<li class="os-navbar-icon-home selected">
<li class="os-navbar-icon-quicklaunch os_quick_start">
<li class="os-navbar-icon-resourcemanager">
<li class="os-navbar-icon-apptray">
<li class="os-navbar-icon-notifications">
<li class="os-navbar-icon-minimise-close">
</ul>
<form class="navbar-search pull-right ui-os-search-form" action="">
<ul class="nav os-navbar-icon os-desktop-navigation" style="left: 407.5px;">
</div>
Resource Manager xpath is not a valid xpath expression.
This should work:
driver.findElement(By.xpath("//li[#class='os-navbar-icon-resourcemanager']")).click()
Use css selector instead of xpath. Also an example of WebDriverWait
WebDriverWait wait = new WebDriverWait(driver,300/*timeout in seconds*/);
By findBy = By.cssSelector("li.os-navbar-icon-resourcemanager");
wait.until(ExpectedConditions.elementToBeClickable(findBy)).click();
you may face "Unable to find element" only in two case for sure.
1.The Element is yet to Load
- Put some wait here.
2. You may try to locate the element wrongly .
- Double Check your Xpath/ID(what ever)
- Make sure you are in the same frame where the element is present.If not, switch to the frame then.
Related
I'm using Selenium Webdriver + Java to do automation tests and stuck with the problem of setting a value in span control.
I have a drop-down with list of clients, the 1st client is selected by default - 'Harry', but I need to change the client to be other than default, e.g. to 'Simon'.
I tried to search here and came with the following code:
Webelement element = driver.findElement(By.xpath("//*[#id=\"SelectedClientDirectorID_chosen\"]/a/span"));
((JavascriptExecutor)driver).executeScript("arguments[0].innerText = 'Simon'", element);
It seems to be fine, the element is found, it's innerText property is changed to 'Simon' while script is being executed.
But as soon as I click 'save' button on the page, the record is saved with the 1st value - 'Harry'.
I could not find the answer on why the new value is not saved.
Html looks like:
<div class="chosen-container chosen-container-single" style="width: 100%;" title="" id="SelectedClientDirectorID_chosen">
<a class="chosen-single" tabindex="-1">
<span class="">Harry</span>
<div><b></b></div>
</a>
<div class="chosen-drop">
<div class="chosen-search">
<input type="text" autocomplete="off">
</div>
<ul class="chosen-results">
<li class="active-result result-selected" style="" data-option-array-index="0">Harry</li>
<li class="active-result result-selected" style="" data-option-array-index="1">Simon</li>
<li class="active-result result-selected" style="" data-option-array-index="2">Robin</li>
<li class="active-result" style="" data-option-array-index="3">Brian</li>
<li class="active-result" style="" data-option-array-index="4">Rupert</li>
<li class="active-result" style="" data-option-array-index="5">Greg</li>
</ul>
</div>
</div>
Appreciate any clue.
Changing innerText value might not bring expected result in your case. You can try to click on drop-down and select required option just like real user do:
driver.findElement(By.xpath("xpathForDropDownButton")).click();
driver.findElement(By.xpath("//li[text()='Simon']")).click();
You might also need to wait some time until required elements become clickable:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpathForDropDownButton"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text()='Simon']"))).click();
Use the following code for the same. Instead of setting innerText, you need to select value from dropdown.
WebElement element = driver.findElement(By.xpath("//ul[#class='chosen-results']/li[normalize-space()='Simon']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
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')]");
I've the below snippet, I need to find the Xpath to click on "Option1"
The Problem here is below two divisions are dynamically displayed based on the browser size or Screen Resolution
<div class="wrap">
<aside id="side-anchor-links">
<ul class="menu">
<li><a class="anchor-active">Option1</a></li>
<li"><a class="">Option2</a></li>
</ul>
</aside>
<main class="content">
<div id="wrapper">
<div id="scroller">
<div id="top-anchor-links" class="scroll-me">
<ul class="menu">
<li><a class="">Option1</a></li>
<li><a class="">Option2</a></li>
</ul>
</div>
</div>
</div>
</main>
</div>
Thanks is Advance.
My Trails:
xpath = //aside[#id="side-anchor-links"]/ul/li[text()="Option1"] //This option will work only when the options are displayed in the side view
xpath = //li[text()="Option1"] //This option will retrieve two occurances
Note: Always class="anchor-active" is associated with option1 in Side View until we click on any option from any division
Try with the below xpath :
//div[#class='wrap']/aside[#id="side-anchor-links"]/ul/li[1]
So, I believe you need to click at one element, and if it is not present - on another.
And if plain //li[text()='Option1'] does not suite you well, try this:
//*[#id='side-anchor-links' or #id='top-anchor-links']//li/a
Better customize your //li/a part, however
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
<div id="sbGlobalNav" class="">
<div id="sbGlobalNavContent">
<div id="sbGlobalNavRightContent">...</div>
<div id="sbGlobalNavLeftContent">
<ul id="globalNavMenu">
<li class="globalNavSeparator firstSeparator">..</li>
<li>..</li>
<li>..</li>
<li>..</li>
<li>..</li>
<li class="globalNavDropdown">
<span>Answer</span>
<ul class="globalNavDropdownContainer" style="display: none;">
<li>..</li>
<li>Daily Polls</li>
<li>..</li>
</ul>
</li>
<li class="globalNavDropdown">..</li>
<li class="globalNavSeparator">..</li>
<li class="globalNavDropdown" id="globalNavRewards">..</li>
<div class="clear"></div>
In this I need to find and click Daily Poll link using Selenium webdriver(java) but I was unable to do it.
What I did is:
Actions builder = new Actions(driver);
builder.moveToElement(driver.findElement(By.xpath("//div[#id='sbGlobalNav']//span[text()='Answer']"))).build().perform();
driver.findElement(By.linkText("Daily Polls")).click();
Check it, whether you have to so something to trigger that element out. Javascript maybe, by mouse over ?
Try this:
driver.findElement(By.xpath("//*[text()='Daily Polls']")).click();
It might work.