Selenium Webdriver unable to click the sub menu item - selenium

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

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 a link by text in Selenium web driver java

I have many links in my page.One with text "Add policyhoder" ,one with text " Add" only and another "Add PP". I need to click link by text .I am using below code to click link having text as "Add" only but it is clicking very first link having "Add" in its text i.e. ""Add PP" available on screen.Please can u help
Driver.findElement(By.linkText("Add"));
My requirement is to click a link with exact text match . for example "Add" here
<td width="100%" colspan="7">
<table width="100%" cellspacing="0" cellpadding="1" valign="bottom">
<input id="hidPoClaim" type="hidden" onblur="ResetScript(this);" onfocus="HighlightScript(this);" value=" PolicySummary " callfunction="" size="" name="/Root/ACORD/InsuranceSvcRs/com.c_HomePolicyInquiryRs/co.cc_AvailableFunctions[com._FunctionName='PoSummary' and com.csc_FunctionName[#Action='ShowPolicyClaims']]">
<tbody>
<tr>
<td width="25%" valign="bottom" colspan="1">
<strong>
<font class="flabel">Policy Claims History:</font>
</strong>
</td>
<td width="20%" valign="bottom" colspan="1">
<font class="flabel"> </font>
<a class="fLabel" onclick="INFCaptureControlID(this); DoLink('POLICYLOSS','','ADD' );return false; " onblur="ResetScript(this);return true;" onfocus="HighlightScript(this);" delimiter="|" screenaction="ADD" href="" screen="Y" objecttype="POLICYLOSS" type="Link" context="Screen">**Add**</a>
</td>
<td width="20%" valign="bottom" colspan="1">
<td align="Center" width="15%" valign="bottom" colspan="1">
<td width="20%" colspan="1">
</tr>
</tbody>
</table
Thanks
Dev
If it the there are 2 elements with the word "Add", Then try something like this:
List<WebElement> list = driver.findElements(By.linkText("Add"));
list.get(1).click();
To find the element by searching for the exact text, then using xpath will be more helpful.
// For "Add" link, according to the HTML you've added to the question
driver.findElement(By.xpath("//a[text()='**Add**']")).click();
I assume because when you click on first link it opens that link page in same tab and can not find second link element as page changed , so you have to open that link page in new tab in.
I think your code would be :
String Newtab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("Add policyhoder")).sendKeys(Newtab);
driver.findElement(By.linkText("Add")).click();
Now above code will work for you.
you can try using
driver.findElement(By.xpath("//a[text()='Add']"));
This would match exact text for you

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

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

Not able to click the button after entering value in textarea Options

code is used is:
WebElement desc=driver.findElementByXPath(".//*[#label='Description']");
desc.sendKeys("testing");
desc.sendKeys(Keys.ENTER);
List<WebElement> button=driver.findElementsByXPath("(//div[#id='sv'])[1]");
for (WebElement buttonname : button)
{
System.out.println("buttonname: "+buttonname.getAttribute("id"));
String but = buttonname.getAttribute("id");
driver.findElementById(but).click();
}
Below is the html code of that textarea and button .
<td>
<textarea id="1992800000" label="Description" ft="12" mand="false"class="ic" maxlength="120" cols="13" rows="2"/>
</td>
......
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 40px; ">
<td class="pdl">
<div class="tbut" onclick="ir('Tas','tas','')" id="sv">Save</div>
</td>
Your XPath can only select one element, so there is no need to create a list and iterate through it.
Try something more like:
WebElement desc=driver.findElementByXPath("//*[#label='Description']");
desc.sendKeys("testing");
WebElement button=driver.findElementsByXPath("(//div[#id='sv'])[1]");
button.click();