Selenium webdriver-java: how to get javascript tooltip - selenium

I want to get tooltip information enabling on JavaScript. I have tried with below code but its shown null every time.
Java Code:
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a"));
action.moveToElement(element).build().perform();
System.out.println(driver.findElement(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a")).getAttribute("data-original-title"));
HTML Code is:
<tr class="single" name="Tasks_59c777d9-8d16-694a-7307-52caad36d751">
<td>
</td>
<td data-type="custom_task_name">
<span class="list" sfuuid="3840" data-original-title="">
<div class="ellipsis_inline" data-original-title="">
Task Testing
<br/>
Toney Harber
</div>
</span>
</td>
I have tried with span tag but this also shows null.

I am providing you 2 options try both.
Option 1
Please try below. It will return you a list. Loop through it. If there is only one span then you can get the element and then get the attribute value.
driver.findElements(By.xpath("//span[#data-original-title]"))
Option 2
Instead of
driver.findElement(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a"))
use findElements
driver.findElements(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a"))
It will return a list of web elements. Loop thorough it you might get more than one element.

Related

How to select a option from the list which is displayed inside 'input' tag instead of 'Select' tag through Selenium WebDriver

Using Selenium Webdriver and Java, I want to select a option from the list which is displayed inside 'input' tag instead of 'Select' tag.
Please find below details-
The GUI is as following-
Please find the GUI image here
<td nowrap="" valign="middle" colspan="3" rowspan="1">
<div class="mceGridField siebui-value mceField">
<input type="text" name="s_6_2_158_0" value="" aria-labelledby="BGC_Type_Label" aria-label="Type" style="height: 24px; width:80px;" class="siebui-ctrl-select siebui-input-popup siebui-align-left siebui-input-align-left ui-autocomplete-input" aria-describedby=" s_6_2_158_0_icon" maxlength="30" tabindex="0" role="combobox" autocomplete="off" data-seq="0" aria-readonly="false">
<span class="siebui-icon-dropdown applet-form-combo applet-list-combo" id="s_6_2_158_0_icon" data-allowdblclick="true"></span></div>
</td>
The GUI List of options are as following-
Please find the GUI image of option list after clicking
I am using below code to select one of the option from the list-
driver.findElement(By.xpath("//*[#id='s_6_2_158_0_icon']")).click();
But still I am not able to select any option.
Can you please help ?
To invoke click() on the desired option from the list you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name^='s_'][aria-labelledby='BGC_Type_Label'][aria-label='Type']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[starts-with(#name,'s_')][#aria-labelledby='BGC_Type_Label' and #aria-label='Type']"))).click();
Did you try sendKeys method btw-
driver.findElement(By.xpath("//*[#id='s_6_2_158_0_icon']")).sendKeys("Fix");
And it's better to use the id -
driver.findElement(By.name("s_6_2_158_0_icon")).sendKeys("Fix");

Get xpath for div inside td tag

I tired a lot to get the xpath of the div which displays as "カーク商品掲載カタログ Online" on screen, but I'm not able to. The application is made using ZK framework hence it has random ID's.
<td z.type="Lic" id="z_mk_qr1" class="activeCatalogVersionTreeItem z-list-cell" z.zcls="z-list-cell" onmouseout="dragHoverClick( $e('z_mk_qr1'), event, true, null, 0);" onmouseover="dragHoverClick( $e('z_mk_qr1'), event, false, 'PerspectiveDND', 500);">
<div id="z_mk_qr1!cave" class="z-list-cell-cnt z-overflow-hidden">カーク商品掲載カタログ Online
<span id="z_mk_rr1" class="catalog-mnemonic-label z-label" z.zcls="z-label"> (STO-O)
</span>
</div>
</td>
I thought the xpath should be something like //div[contains(text(),'カーク商品掲載カタログ Online')], but it doesn't work.
Thanks!
Try the below cssSelector to identify the required element.
td.activeCatalogVersionTreeItem div.z-list-cell-cnt
You may use partial text match in this case as follows:
//div[contains(text(), 'Online')]
Check the link below for more:
https://sqa.stackexchange.com/questions/10342/how-to-find-element-using-contains-in-xpath

Unable to locate the 'Edit' link by using any type of locators in the below snippet

I tried almost every type of locators like xpath, CSS, linktext etc. Please see below snippet:
<td class="actionColumn">
<a class="actionLink" title="Edit - Record 1 - Sarika Mahurkar" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="1" data-seclkp="/50011000008fykL/e" data-seclki="50011000008fykL" data-seclkh="afd7fe8d662a1ff5858a5a5ca316d1ff" data-seclke="Case" href="javascript:srcUp(%27%2F50011000008fykL%2Fe%3FsrPos%3D0%26srKp%3D500%26retURL%3D%252F_ui%252Fsearch%252Fui%252FUnifiedSearchResults%253FsearchType%253D2%2526str%253D02518285%2526isdtp%253Dvw%2526isWsVw%253Dtrue%2526nonce%253D3c705d75cbfb6776b726b3a59cf3e9aa83e1ac47fe50b2b8b3c47adff9e3484a%2526sfdcIFrameOrigin%253Dhttps%25253A%25252F%25252Fadidas--rfcuat.cs18.my.salesforce.com%2526initialViewMode%253Dsummary%2526asPhrase%253D1%2526searchOwner%253D0%2526saveOptions%253D1%26isdtp%3Dvw%27);">Edit</a>
</td>
`
Edit
xPath based on Edit word in title of action
//td[#class='actionColumn']/a[contains(#title, 'Edit')]
Also you can add condition that a element has actionLink as a class
//td[#class='actionColumn']/a[contains(#title, 'Edit') and #class='actionLink']

I am unable to click on a Web Element using Selenium Webdriver

I am trying to automate an application using selenium Webdriver. I frequently come across scenarios where click action does not work on the WebElements.
For instance , in the html block that looked something like :
<div id=staticid>
<table>
<tbody>
<tr id="tr1" class="class1" tabindex="-1" data-recordindex="0" data-recordid="1" data-boundview="gridview-1211" role="row">
<td id="td1" class="class2 " role="gridcell">
<div class="class3 " style="text-align:left;" unselectable="on">Content 1</div>
<td id="td2" class="class6" role="gridcell">
<div class="class5" style="text-align:center;" unselectable="on">
<img class="class4" src="chekboximage">
</div>
</td>
<td id="td3" class="class8" role="gridcell">
<div class="class5" style="text-align:center;" unselectable="on">
<img class="class4" src="checkboximage">
</div>
</td>
</tr>
</tbody>
</table>
</div>
The ids are dynamically generated and the classes are like "grid-inner.." so on. (I ve replaced them to make it smaller)
The last two elements appear as checkboxes.
On clicking the block, the class of the td block changes and the the checkbox appears to be clicked.
I tried to click on the checkboxes using driver.findElement(By.xpath()),csspath and everything. The click action seems to be carried out successfully but the desired result,that is ,the checkbox being checked does not happen.
I verified my xpath and csspaths using Selenium IDE. The Element gets located correctly using the "Find" option. Click action also gets performed ,but the checkboxes remain unchecked.
I ve been trying to get this done for almost a week now.
Can anyone please help me out?
The code did seem to change the image on click (I compared the image source before and after a click) . But there were no visible "onclick()" calls (I am not sure if there are other ways of invoking java script fuctions on click.Please do let me know if there are ). Anyway , I tried clicking on the block. it works with Se-IDE and Se-IEDriver but not in FireFoxDriver.
Try driver.findElement(By.xpath()).sendKeys(Keys.ENTER); this also does the same as click but with keyboard stroke. Hope it works.

How can I select checkboxes using the Selenium Java WebDriver?

How can I check the checkboxes using an id or XPath expression? Is there a method similar to select by visibletext for a dropdown?
Going through the examples given for all other related questions, I could not find a proper solution that works in a concise way that by few line or method I can check a chekbox or radio button.
A sample HTML section is below:
<tbody>
<tr>
<td>
<span class="120927">
<input id="ctl00_CM_ctl01_chkOptions_0" type="checkbox" name="ctl00$CM$ctl01$chkOptions$0"/>
<label for="ctl00_CM_ctl01_chkOptions_0">housingmoves</label>
</span>
</td>
</tr>
<tr>
<td>
<span class="120928">
<input id="ctl00_CM_ctl01_chkOptions_1" type="checkbox" name="ctl00$CM$ctl01$chkOptions$1"/>
<label for="ctl00_CM_ctl01_chkOptions_1">Seaside & Country Homes</label>
</span>
</td>
</tr>
</tbody>
Selecting a checkbox is similar to clicking a button.
driver.findElement(By.id("idOfTheElement")).click();
will do.
However, you can also see whether the checkbox is already checked. The following snippet checks whether the checkbox is selected or not. If it is not selected, then it selects.
if ( !driver.findElement(By.id("idOfTheElement")).isSelected() )
{
driver.findElement(By.id("idOfTheElement")).click();
}
It appears that the Internet Explorer driver does not interact with everything in the same way the other drivers do and checkboxes is one of those cases.
The trick with checkboxes is to send the Space key instead of using a click (only needed on Internet Explorer), like so in C#:
if (driver.Capabilities.BrowserName.Equals(“internet explorer"))
driver.findElement(By.id("idOfTheElement").SendKeys(Keys.Space);
else
driver.findElement(By.id("idOfTheElement").Click();
If you want to click on all checkboxes at once, a method like this will do:
private void ClickAllCheckboxes()
{
foreach (IWebElement e in driver.FindElements(By.xpath("//input[#type='checkbox']")))
{
if(!e.Selected)
e.Click();
}
}
Solution for C#
try
{
IWebElement TargetElement = driver.FindElement(By.XPath(xPathVal));
if (!TargetElement.Selected)
{
TargetElement.SendKeys(Keys.Space);
}
}
catch (Exception e)
{
}
You can use the following code:
List<WebElement> checkbox = driver.findElements(By.name("vehicle"));
((WebElement) checkbox.get(0)).click();
My HTML code was as follows:
<.input type="checkbox" name="vehicle" value="Bike">I have a bike<br/>
<.input type="checkbox" name="vehicle" value="Car">I have a car<br/>
To get the checkbox for 'Seaside & Country Homes', use this XPath:
//label[text()='Seaside & Country Homes']/preceding-sibling::input[#type='checkbox']
To get the checkbox for 'housingmoves', use this XPath:
//label[text()='housingmoves']/preceding-sibling::input[#type='checkbox']
The principle here is to get the label with the text you want, then get the checkbox that is before the label, since that seems to be how your HTML is laid out.
To get all checkboxes, you would start a little higher up and then work down, so that is to say get the table, and then get any checkbox within a span:
//table/descendant::span/input[#type='checkbox']
I found that sometimes JavaScript doesn't allow me to click the checkbox because was working with the element by onchange event.
And that sentence helps me to allow the problem:
driver.findElement(By.xpath(".//*[#id='theID']")).sendKeys(Keys.SPACE);
This should help -
IWebElement elementToClick = driver.findElement(By.xpath(""//input[contains(#id, 'lstCategory_0')]"));
elementToClick.Click();
You can also pass an id.
If you want something like visible text you can "find element" by name if they have names.
The below code will first get all the checkboxes present on the page, and then deselect all the checked boxes.
List<WebElement> allCheckbox = driver.findElements(By
.xpath("//input[#type='checkbox']"));
for (WebElement ele : allCheckbox) {
if (ele.isSelected()) {
ele.click();
}
}
A solution using WebDriver and C# is below. The key idea is to get the ID of the checkbox from the labels' 'for' attribute, and use that to identify the checkbox.
The code will also set the checkbox state only if it needs to be changed.
public void SetCheckboxStatus(string value, bool toCheck)
{
// Get the label containing the checkbox state
IWebElement labelElement = this.Driver.FindElement(By.XPath(string.Format("//label[.='{0}']",value)));
string checkboxId = labelElement.GetAttribute("for");
IWebElement checkbox = this.Driver.FindElement(By.Id(checkboxId));
if (toCheck != checkbox.Selected)
{
checkbox.Click();
}
}
Maybe a good starting point:
isChecked = driver.findElement((By.id("idOftheElement"))).getAttribute("name");
if(!isChecked.contains("chkOptions$1"))
{
driver.FindElement(By.Id("idOfTheElement")).Click();
}
Running this approach will in fact toggle the checkbox; .isSelected() in Java/Selenium 2 apparently always returns false (at least with the Java, Selenium, and Firefox versions I tested it with).
The selection of the proper checkbox isn't where the problem lies -- rather, it is in distinguishing correctly the initial state to needlessly avoid reclicking an already-checked box.
To select a checkbox, use the "WebElement" class.
To operate on a drop-down list, use the "Select" class.
Step 1:
The object locator supposed to be used here is XPath. So derive the XPath for those two checkboxes.
String housingmoves="//label[contains(text(),'housingmoves')]/preceding-sibling::input";
String season_country_homes="//label[contains(text(),'Seaside & Country Homes')]/preceding-sibling::input";
Step 2:
Perform a click on the checkboxes
driver.findElement(By.xpath(housingmoves)).click();
driver.findElement(By.xpath(season_country_homes)).click();
For a partial match, do the following:
getDriver().findElement(By.cssSelector("<tag name>[id*='id pattern to look for']")).click();
Here is the C# version of Scott Crowe's answer. I found that both IEDriver and ChromeDriver responded to sending a Key.Space instead of clicking on the checkbox.
if (((RemoteWebDriver)driver).Capabilities.BrowserName == "firefox")
{
// Firefox
driver.FindElement(By.Id("idOfTheElement")).Click();
}
else
{
// Chrome and Internet Explorer
driver.FindElement(By.Id("idOfTheElement")).SendKeys(Keys.Space);
}
I tried with various approaches, but nothing worked. I kept getting "Cannot click element" or ElementNotVisibleException.
I was able to find the input, but I couldn't check it. Now, I'm clicking on the div that contains the checkbox and it works with following HTML (CSS based on Bootstrap).
#foreach (var item in Model)
{
<tr>
<td>
<div id="#item.Id" class="checkbox">
<label><input type="checkbox" class="selectone" value="#item.Id"></label>
</div>
</td>
<td val="#item.Id">
#item.Detail
</td>
<td>
<div>#item.Desc
</div>
</td>
<td>
#Html.ActionLink("Edit", "Create", new { EditId = item.Id })
</td>
</tr>
}
This is the code for WebDriver:
var table = driver.FindElement(By.TagName("table"));
var tds = table.FindElements(By.TagName("td"));
var itemTds = tds.Where(t => t.Text == itemtocheck);
foreach (var td in itemTds)
{
var CheckBoxTd = tds[tds.IndexOf(td) - 1];
var val = td.GetAttribute("val");
CheckBoxTd.FindElement(By.Id(val)).Click();
}
In this approach, I give the item id as id for the div and also add an attribute for td with that id. Once I find the td of the item that needs to be checked, I can find the div before that td and click it. We can also use the XPath query that supports before (here is the example http://learn-automation.com/how-to-write-dynamic-xpath-in-selenium/).