How to click the image link by selenium webdriver? - selenium

I have the following html page source, and I tried to click the image by xpath/cssselector. none can work, could you help me to find the issue with my code? I use IE9.
<pretable border="0" cellpadding="0" cellspacing="0" width="700">
<tr>
<td rowspan="2" width="120">
<a href="#" onclick="oCMenu.m['top1'].b.moveIt(8,60); oCMenu.showsub('top1'); "
onclick="return false" class="FontNormal">
<img border="0" src="images/shim.gif" width="112" height="73"></a></td>
</tr>
</pretable>
my code is:
ieDriver.findElement(By.xpath("//html/table/tr[1]/td/a[#class='FontNormal']/img[#src='images
/shim.gif']")).click();
ieDriver.findElement(By.cssselector("class='FontNormal'")).click();

With the available DOM Structure we can always go with CSS Selector.
CSS Selectors
css=a[href='images/shim.gif']
css=a[href*='shim.gif']
Then Perform
driver.findElement(By.cssSelector("a[href='images/shim.gif']")).click();
OR
driver.findElement(By.cssSelector("a[href*='shim.gif']")).click();

You are trying to provide a full xpath and yet it does not match up with your html provided.
'table' is not the same as 'pretable', and you don't need to supply the full path anyway. Instead just try this for your xpath:
XPath("//a[#class='FontNormal']")
Here, xpath will search for any link with the class 'FontNormal' attached. If there is only one, this will select your element. If there is more you may need to be more specific.

First write proper xpath to identify that webelement where you want to perform click operation
driver.findElement(By.Xpath("//img[# src='images/shim.gif']")).click();

element(By.xpath('//html/table/tr[1]/td/a[#class='FontNormal']/img[#src='images
/shim.gif']')).click();

Use Firebug with Firepath for Mozilla Firefox. This will automatically generate xpath for you.
Webelement element = driver.findElement(By.xpath("*xpath here*");
element.click();

click on image link by using xpath.
driver.findElement(By.xpath("//img[contains(#src,'/static/images/image_name.png')]")).click();
You can change the image path depending on your image source.

Related

how to find xpath of the input box in the following HTML

I have a html like this
<td class="random">
<div id="randomID">Product:</div>
</td>
<td class="random">
<div id="randomID">
<input type="text" class="random">
</div>
</td>
my xpath //div[contains(text(),"Product:")] gives me the first element for which I want to send input. How do I get the input xpath so I can do input.sendkeys() on it.
You use either of the xpath to get the input tag.
Use following and index of input tag
//div[contains(text(),"Product:")]/following::input[1]
Or find the td tag and then use following-sibling
//td[.//div[contains(text(),"Product:")]]/following-sibling::td[1]//input
You can use below xpath as well.
//div[text()="Product:"]/following::input[1]
Or
//td[.//div[text()="Product:"]]/following-sibling::td[1]//input
Use following-sibling
Try with following xpath:
//td[contains(.,"Product:")]//following-sibling::td//input
For the provided code, the input is very simple to identify:
//input[#class='random']
Since I can't see any other duplicate code to force you to be more precise, I don't see the problem in using this one.
If you have multiple , and each or some of them have an input inside, in this case:
//div[text()="Product:"]/following::input[1]
Where Product is the visible text of the div that you want to use as a parent, and then you go straight in the first input taken by its index.

Trouble generating the right Xpath

My requirement is when I click on Facebook it should open the Facebook homepage. The xpath which I generated is //*[#class='sub2']/tbody/tr[2]/td/a
but still it is giving me NoSuchElementException. Please help me out in generating the correct xPath.
<div>
<center>
<table width="100%" class="sub2" style="float: none" border='8'
cellspacing="8" cellpadding="8">
<tbody>
<tr>
<th>
<center>Sample Program</center>
</th>
</tr>
<tr>
<td>
<a href="https://facebook.com">
<center> Facebook </center>
</a>
</td>
</tr>
<tr> </tr>
</tbody>
</table>
</center>
You can use XPath's normalize-space() as in //a[normalize-space()="Facebook"]
You can fetch the element by using the xpath:
//a[#href='https://facebook.com']
To locate the element with text as Facebook you can use either of the following Locator Strategies:
Using CssSelector:
"table td>a[href*='facebook']>center"
Using Xpath:
"//table//td/a/center[normalize-space()='Facebook']"
Reference
You can find a couple of detailed discussions on NoSuchElementException in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
NoSuchElementException, Selenium unable to locate element
It seems that you have messed up absolute xpath with relative xpath. if you start by using // it means you are using relative xpath. // means it can search the element anywhere at the webpage. Since you are trying to find a tag with the text 'Facebook' you can try //a[contains(text(),"Facebook")] which means select an anchor tag within the whole DOM which contains the text "Facebook".
Or you can try //*[contains(text(),"Facebook")] if you are quite sure there are no other elements containing the text 'Facebook', since the above xpath means "select all elemnts in the DOM where it contains the text 'Facebook' ".

Robot Framework - Unable to find filter xpath in Kendo grid and add text

I am attempting to create a Robot Framework Selenium test where I input text into a Kendo grid filter and then check the results. The grid is here:
The filter code is here:
<thead class="k-grid-header" xpath="1">
<tr>...</tr>
<tr>
<td class="rowFilterContainer"></td>
<td class="rowFilterContainer"></td>
<td class="rowFilterContainer" data-field="UserId"></td>
<td class="rowFilterContainer" data-field="CandidateId">
<input class="rowFilter" data-field="CanddateId" data-type="string" type="text" data-
operator="startswith">
</td>
I am attempting to locate and then enter text in the CandidateId field using Selenium with an xpath of "//div[#id='Tab_0']//td[4]//input[1]" but when I run the test if complains that it cannot find it.
Any help on how to find this field (with a better xpath) and to add the text would be greatly appreciated. I have tried using CSS as well.
Try this xpath:
//td[#data-field='CandidateId']/input
Try this:
(.//thead[#class='k-grid-header']/tr)[2]/td[4]/input[#class='rowFilter']

Unable to find correct Target selector to click an element

Please see the image below which shows code of the web page in Developer Tools window and advise me the correct selector for 'Depreciation Models'. Text highlighted in red is constant, however the surrounding text is dynamic. So I tried using contains selector to locate but unsuccessful. I want to avoid XPATH as number of before and after div elements may keep changing.
I am using Selenium IDE hence the C#/Java code for RC/Webdriver won't help much.
Selenium IDE generated target path is this:
css=#dhxId_rgWATog7lC3E_27572|6059|6152 > td.sub_item_text > div.sub_item_text
I tried
css=contains('27572') > td.sub_item_text > div.sub_item_text
but it didn't work.
Kindly suggest. I am stuck. Thanks.
As you tried the Locator Strategy as :
css=contains('27572') > td.sub_item_text > div.sub_item_text
Reasons for not working
From the snapshot of the HTML you have provided, 27572 is not the innerText but partial string of the id attribute.
As per selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”:
The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).
Solution
You can use the following xpath as per the existing DOM Tree:
xpath=//tr[#class='sub_item'][contains(#id,'27572')]//td[#class='sub_item_text']/div[#class='sub_item_text'][contains(.,'Depreciations Models')]
I guess this would be the right approach:
<style>
[id*="27572"] > td.sub_item_text > div.sub_item_text{
color:red;
}
<table>
<tbody>
<tr id="dhxId_rgWATog7lC3E_27572|6059|6152" class="sub_item">
<td class="sub_item_icon">
<i class="fa fa-user epc-down-circled-2"></i>
</td>
<td class="sub_item_text">
<div class="sub_item_text"> Depriciations Models</div>
</td>
</tr>
</tbody>
</table>
It will set all elements that have an id attribute value containing "27572" and inside of it.

How to find an item in Selenium WebDriver?

I want to find the following item using Selenium. The value of the class changes whenever there is a change. This is inside a complex page (multiple iframes, and other items loaded dynamically). The only unique id is itemid, which is dynamic value and title combination. If I click on this Action, am getting another new set of complex items. I am new to Selenium. How to do that?
HTML:
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button">Actions <img src="../row.gif"></td>
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button button-active">Actions <img src="../row.gif"></td>
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button button-hover">Actions <img src="../row.gif"></td>
The code I tried:
Find by Xpath
var element=driver.FindElement(By.XPath("html/body/div[id='pageContent']/iframe/#document/ht‌ml/frameset/frame[name='detailsDisplay']/#document/html/body/form[name='tableForm‌']/div[id='divToolbarContainer']/div[id='divToolbar']/div[1][class='toolbar']/tab‌​le/tbody/tr/td[title='Actions']"));
Find by Link Text
var element = driver.FindElement(By.LinkText("Actions"));
Any help would be appreciated.
Try
By.CssSelector("td[title="Actions"]");
By.CssSelector("td[itemid="xxyyy.as123"]");
By.CssSelector("td[itemid="xxyyy.as123"][title="Actions"]")
Create Dynamic CSS Selector.
For Example:
driver.FindElement(By.CssSelector("td[itemid$="xxyyy."]")).Click();
Note: In dynamic Elements, there is always a part of locator wich is fixed. we need to generate the locator using this part.
If fixed part is at starting - Use Carrot Character (^)
If fixed part is at Middle - Use Asterisk sign (*)
If fixed part is at End - Use Doller sign ($)
Finally I was able to achieve it, by using the frame names.
driver.SwitchTo().Frame("content").SwitchTo().Frame("detailsDisplay");
var element = driver.FindElement(By.XPath("//*[#id=\"divToolbar\"]/div[1]/table/tbody/tr/td[1]"));
Thanks everyone.