Unable to Click on Element - selenium

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).

Related

Webdriver FindElement by CssSelector Click Not Working

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();

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()

How to search by first td text and clicking in seconds td href by JUnit, Webdriver?

<div class="row">
<div class="row">
<table>
<tbody>
<tr>
<td class="list-text">
<div style="background-color: transparent;">Testing this function</div>
</td>
<td class="list-buttons" rowspan="2">
Config
<a class="edit-button" href="javascript:;">Edit</a>
<a class="save-button" href="javascript:;" style="display:none;">Save</a>
Delete
</td>
</tr>
<tr>
</tbody>
</table>
</div>
I need to find the element which contains the text "Testing this function" and click to link "Config".
Can't find a way to do it.
Edited: Found a way to do it:
By.xpath("//div[#class='row']/table/tbody/tr[td/div[contains(.,'Testing')]]/td[2]/a[contains(.,'Config')]")
But maybe someone has a better solution for this?
If you have to use text, my preference would be;
By.xpath("//div[contains(text(),'Testing')]]//a[contains(text(),'Config')]") .
I prefer using "text()" to "." just for readability. Apologies if "." is actually a better approach which I wasn't aware of.
Also unless you have a specific reason, I would avoid using tightly coupled xpath. E.g. I have not used " td[2]", instead I have said the"a" can be anywhere within in the "div"

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