Webdriver FindElement by CssSelector Click Not Working - selenium

I have a dropdown on a web page for selecting a country which has been rendered using the jQuery Chosen plugin. An extract of the html below,
<div>
<label for="phMainContent_EmployeeAdd1_ddlCountry" id="phMainContent_EmployeeAdd1_lblCountry" class="short required">Country*</label>:
<div id="phMainContent_EmployeeAdd1_ddlCountry_chzn" class="chzn-container undefined chzn-container-single" style="width: 199.44444px;">
<span>Please select ...</span><div><b></b></div>
<div class="chzn-drop" style="left: -9000px; width: 197.222px; top: 28px;">
<div class="chzn-search"><input type="text" style="width: 162px;"></div>
<ul class="chzn-results">
<li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_0" class="active-result result-selected">Please select ...</li>
<li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1" class="active-result">United Kingdom</li>
<li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_2" class="active-result">Afghanistan</li>
.......
If I use the Selenium IDE to record the actions to select the “United Kingdom” from the list the following script is recorded. Run snippet to see table with the commands in it.
<table border="1">
<tr>
<td>Command</td>
<td>Target</td>
</tr>
<tr>
<td>click</td>
<td>css=a.chzn-single > span</td>
</tr>
<tr>
<td>click</td>
<td>id=phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1</td>
</tr>
</table>
I can run this script repeatably in the IDE and the UK is selected from the dropdown each time. However, if I export the C#/Nunit/Webdriver code below
driver.FindElement(By.CssSelector("a.chzn-single > span")).Click();
driver.FindElement(By.Id("phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1")).Click();
and execute it, it fails on the 1st statement with the Selenium Element Not Visible exception.
Any advice on how to resolve this issue?

you can try xPath and select like //span[contains(.,'Please Select')]

Use explicit wait to make sure the dropdown is visible before the click
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement dropdown = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("a.chzn-single > span")));
dropdown.Click();
driver.FindElement(By.Id("phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1")).Click();

Related

xpath with Multiple condition in Selenium

Wants to locate element with multiple condition on particular row,
I want to Click on div which contains ng-click="condition001" which has parent link contains Auto-001
So query become like : Select element xpath("//div[contains(#ng-click,'condition001')]") where Link("Auto-001")
<tr ng-repeat="(abc, xyz)" ng-show="data.length > 0">
<td class="x001">
<div class="x002">
<a class="x003" href="#">Auto-001</a>
</div>
</td>
<td class="x004">
<div class="x005">
<div class="x006">
<div class="x007" ng-click="condition001" tabindex="0">
<i class="x008"></i>
</div>
</div>
</div>
</td>
</tr>
Please suggest relevant xpath code which can work for above criteria,
To click on the WebElement identified through xpath as ("//div[contains(#ng-click,'condition001')]") where it has partialLinkText as ("Auto-001") you can use the following line of Java code :
WebElement myElem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='x003' and contains(.,'Auto-001')]//following::td[1]//div[#class='x007' and #ng-click='condition001']")));

How to click on this javascript element using selenium webdriver

<table>
<tbody>
<tr>
<th colspan="9">
<span id="pageText">
<a style="cursor:pointer;" onclick="javascript:setData(2,true)"><</a>
<a style="cursor:pointer;" onclick="javascript:setData(2,false)">></a>
</span>
</th>
</tr>
</tbody>
</table>
I tried this, but it does not work:
((JavascriptExecutor)driver).executeScript("document.getElementById('>').click()");
i want to click on ">" link.
First of all there is no id >. For id attached to an element see what is contained in the id attribute of the element, what u provided is the innerHTML or TextContent of the element.
Secondly you can achieve it using webdriver functions also as:
driver.findElement(By.xpath("//span[#id='pageText']/a[contains(.,'>')]")).click();

Selenium Webdriver unable to click the sub menu item

I am able to traverse through the menu items, but the final element is not clicked by WebDriver.
My code snippet:
WebElement hover0 = driver.findElement(By.id("td_Menu_0"));
WebElement hover = driver.findElement(By.xpath(".//*[#id='role6_Maintain']/table/tbody/tr/td[1]"));
action.moveToElement(hover0).moveToElement(hover);
action.moveToElement(driver.findElement(By.cssSelector("#menuClickable_0_6_0_0")))
.click().build().perform();
The final WebElement HTML:
<td onkeydown="return menuClickableOperation(this,event);"
onclick="javascript:deleteGrpWindowNode('menu_Maintain',0,'br_w_BusissPartner','BRGUI','Business Partner','','','HJHF');"
onmouseout="menuDeSelect(this);" onmouseover="menuSelect(this)"
onmousemove="DisplayIFrame();" tabindex="11" id="menuClickable_0_6_0_0"
class="menuNormal2">
<table width="100%">
<tbody>
<tr>
<td width="100%" style="">
<p title="Business Partner" class="MenuTxt">Business Partner</p>
</td>
</tr>
</tbody>
</table>
</td>
Try this code instead:
action.moveToElement(hover0).build().perform();
action.moveToElement(hover).build().perform();
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#menuClickable_0_6_0_0")));//Waiting for 20 seconds for the final element to be visible.
action.moveToElement(driver.findElement(By.xpath("//td[#id='menuClickable_0_6_0_0']//p[.='Business Partner']"))).click().build().perform();
As you are already hovering on hover0 and hover, i am hoping the third object is available.
So why cant you perform direct click on the third object like
driver.findElement(By.xpath("//td[#id='menuClickable_0_6_0_0']//p[.='Business Partner']").click
The other way around your way is like
action.click(yourElement).build().perform()

Unable to Click on Element

I am unable to click on the element. I am able to locate it with xpath, the object with statements
"element.getText()"
returns correct values
"element.isDisplayed()"
returns true
but when i say "element.click()" throws an exception
"Element is not currently visible and so may not be interacted with"
The above exception is thrown in selenium 2.34 and higher versions.
When I use older selenium version say "2.25" it doesn't throw an exception but the click has no effect.
I am using FF browser on Win7 machine.Below is the HTML
<div class="dojoxGrid-row dojoxGrid-row-over row-read" style="">
<table class="dojoxGrid-row-table" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="dojoxGrid-cell " style="width:36px;" idx="0" tabindex="-1">
<td class="dojoxGrid-cell gridColFrom " style="width:150px;" idx="1" tabindex="-1">
<td class="dojoxGrid-cell gridColType " style="width:16px;" idx="2" tabindex="-1"/>
<td class="dojoxGrid-cell gridColAttach " style="width:16px;" idx="3" tabindex="-1"/>
<td class="dojoxGrid-cell gridColSub dojoxGrid-cell-over" style="width:400px;" idx="4" tabindex="-1">
<span style="white-space: nowrap;" title="(No subject)">(No subject)</span>
</td>
<td class="dojoxGrid-cell " style="width:72px;" idx="5" tabindex="-1">Tue May 13</td>
<td class="dojoxGrid-cell gridColHov " style="width:16px;" idx="6" tabindex="-1">
<td class="dojoxGrid-cell gridColFlag " style="width:16px;" idx="7" tabindex="-1">
</tr>
</tbody>
</table>
</div>
Element is not currently visible and so may not be interacted with
What you're seeing is commonly caused by multiple elements on the screen matching the search criteria. WebElement.findElement returns the first match, which may not actually be visible. You can check by calling "isDisplayed" on it.
Try to locate that element using same xpath in firebug and check whether it is showing element which is visible on screen.
Try to click element using jsExecuter:
JavascriptExecutor myJSExecutor = (JavascriptExecutor)myDriver;
myJSExecutor.executeScript("arguments[0].click();", myElement);
//where myDriver and myElement are already defined WebDriver and WebElement
It seems your application uses Dojo for UI. I faced same problem while clicking on an element which has dojo property and I solved it using JavaScriptExecutor in similar way as you mentioned above. The other solution to this problem is use .sendKeys(Keys.Enter).

Facing issues in Identify button in selenium

I am working on 1 project, where i have to locate button, i have used xpath for same, but button id change on every refresh, so i am facing problem in it..
Below is screenshot for same - let me know you can i identify that button so that it will not cause error even if id chages
<button type="button" id="ext-gen11" class=" x-btn-text">Login</button>
Code HTML
<tr>
<td></td>
<td><div id="LoginButton" style="float: left;"><table style="width: auto;" id="ext-comp-1032" class="x-btn x-btn-noicon x-btn-over x-btn-focus" cellspacing="0"><tbody class="x-btn-small x-btn-icon-small-left"><tr><td class="x-btn-tl"><i> </i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i> </i></td></tr><tr><td class="x-btn-ml"><i> </i></td><td class="x-btn-mc"><em class="" unselectable="on"><button class=" x-btn-text" id="ext-gen12" type="button">Login</button></em></td><td class="x-btn-mr"><i> </i></td></tr><tr><td class="x-btn-bl"><i> </i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i> </i></td></tr></tbody></table></div></td>
</tr>
xPath:
//button[text()='Login']
XPath should do the trick:
Command: clickAndWait
Target: //input[#value='Login']
Edit
Assuming, that there is only one button with value "Login" on it