Selenium RC: Need to click <td> - selenium

We have selection code that is triggered when the empty space in a TD is clicked. The "onclick=..." is on the TR.
When I get the parent(TD) of an element and click it, it triggers the click on the element and not the TD or TR. So, if the base element is a link, it triggers it. If the base element is something without an onClick event it does nothing.
I check the tag name of the element that is being clicked and I know that it is the TD. I've also tried to click the row by going up twice, same result.
Any ideas how to trigger the click event on the row???
Example HTML:
<table>
<tr onclick="selectRow(this)";>
<td>Link Text</td>
<td><input id="someID" type="text" value="display"></td>
</tr>
</table>

For select ANY element inside of document you can use full or partial xpath.
As example:
"/html/body/div[5]/div[2]/div/div[3]/div[6]/div/table/tr[1]/td[2]"
or
".//table/tr[1]/td[2]"
and after click as on any another element =)
Full xpath you can generate automatically from firebug/dragonfly/chrome development tools by right click on any tag and select "copy xpath"

The possible issue is NOT the selector. Issue is the SIZE of the element. Selenium will not click on an element that is 0 in height/width. The reason the a works is because that has some innerText and size is not 0. Hope it makes sense.

I found a work-around by generating a JavaScript command that clicks the TD element. Not sure why it works that way but not through the driver, but it does work.
We use Prototype, so if I have the ID:
$('some_id').up().click()
If I have the text of a link:
$$("a:contains('Link Text')")[0].up().click()

Related

Unable to derive Xpath syntax for radio buttons without any distinguished value inside div tag

I want to click a radio button out of 10 radio buttons on a webpage but each radio button tag is exactly same inside Div/INPUT tag, i.e for each radio button values are exactly same and hence not able to derive a xpath to click on it. Can't use contains text to levarage radio button name as radio button name is in different SPAN tag so can't use that as a reference, Please help me: below is the code :
<div class="classname">
<input name="category.value" type="radio" class="classname">
</input>
<span class="classname">Radio button Name</span>
driver.find_element_by_xpath("//span[#class='classname']/input[#value=1]").click()
Your HTML must have some kind of return type, maybe value or name. I belive you can use that.
Mentioned in the comments can you please try:
//span[text()='Radio button Name']/preceding-sibling::input[1]
Not that this is case sensitive.
This works based on the html provided:
If it doesn't work for you can you please verify your HTML. There are lots of other ways of getting the element. If you need to modify the html just do it and let us know :-)

Behat to click on element by class or ID name?

Is it possible for behat to find an element by class name to click on? It looks like only the following is searched for: id|name|title|alt|value
For example, how could you successfully identify this element to click on?
<a class="button medium round signup" href="http://link.com" data-reveal-id="signupModal">sometext</a>
Also here is a simple page with a button, that has an ID. How come the following does not access the button?
<button id="myBtn" type="button" onclick="myFunction()">Try it</button>
Given I am on "http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_button_form"
When I press "myBtn"
Thanks
The button can not be accessed because it is contained in an iframe, you need to switch to that iframe to make the button available.
You can create steps to click on an element by any type of identifier (class included), or you can implement new step that requires css/xpath selectors.
Here you can find an example of a method to click by selector.
The first HTML code is nothing but a HREF. Behat should be easily able to identify it by using the line below:
Given I follow "sometext"
If the above doesn't work, you can simply use the CSS selector and click the element:
$locator = '.';
$this->getSession()->getPage()->find('css',$locator)->click();

Having trouble clicking on a button/link with Selenium Webdriver

I'm having real trouble clicking on this "button" link in this web app. It's defined as so:
<tr>
<td id="mainleftlinkzoneover" width="9" valign="top">
<td id="mainleftlinkzoneover" class="mainleftlinks" width="151" title="Online Training"> Online Training</td>
</tr>
Notice how there is no name or ID to use. Thanks! I've tried clicking it by Link Text. Nope. I've tried clicking it by partial link text. Nope.
Not sure why that isn't working but I've only been doing Selenium for a whole day. I'm successfully logging into the application so things are working. If I use By.PartialLinkText it doesn't fail but it doesn't click the link either.
WebElement OnlineTrainBtn = driver.findElement(By.partialLinkText("Training"));
OnlineTrainBtn.click();
I think I may be left with either Xpath or CssSelector but I have no idea how to write the search string.
Any help on this is greatly appreciated, I have ten or twelve more "buttons" like this to deal with.
The By.PartialLinkText searches among the <a> tags.
So in your case, I would use an CSS selector to match the title:
driver.findElement(By.cssSelector("td[title='Online Training']")).click();
You could also use an XPath to partially match the text:
driver.findElement(By.xpath("//td[contains(., 'Training')]")).click();

selenium click using span class or onclick

I am a newbie to java and selenium webdriver. I am having an issue clicking an image. Below is the page source.
<a href="javascript:void(0);">
<span class="HomeButton" onclick="javascript:onBtnHomeClick();"/>
</a>
I tried below codes but did not work and still getting the Unable to locate element error.
driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("HomeButton"))).click();
I have to click the homebutton. Any help would be much appreciated
I don't know why By.className("HomeButton") didn't work but you have errors in the other two.
In driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click(); the tag for onclick is <span> not <a>. It also not onBtnHomeClick() but javascript:onBtnHomeClick();
driver.findElement(By.xpath("//span[#onclick='javascript:onBtnHomeClick();']")).click();
If you want to use onBtnHomeClick() use contains
driver.findElement(By.xpath("//span[contains(#onclick, 'onBtnHomeClick')]")).click();
Or
driver.findElement(By.cssSelector("onclick*='onBtnHomeClick'")).click();
And in wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click(); the <span> parent tag is <a>, not <div>
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/a/span"))).click();
You simply need the correct locator IF your element will be eventually visible.
Xpath = "//span[contains(#class,'HomeButton') and contains(#onclick,'onBtnHomeClick')]"
Add wait as needed above exanmple, that should work.

Can Python selenium be used to click on webpage labels?

I am working on third party applications where on the page too many labels are used as link or navigate to another page. So does python can do such tasks or read the page select list values?
I am using python 2.7 and selenium.
EDIT
There is a label called configuration see the corresponding code below:
<td class="tabtext-unsel"><nobr>Configuration</nobr></td>
<td class="unselected_tab_mid"><img src="/simpletrade/default/images/background/spacer.gif" width="16" height="25"></td>
Thanks,
Yes, it can. A label is implemented in HTML as <LABEL FOR="field_id">label text</LABEL>, and you can use it in an XPath locator in Selenium.
(Update after edit of question)
That's not a label in HTML, but it's still possible using an XPath locator. It's not even particularly difficult. Both <TD> elements are children of the same <TR> element, so something like this will do the job: //tr[td/a[.='Configuration']]/td/img will match the <IMG> element. But if all you want to do is locate the link, it's even easier: //tr/td/a[.='Configuration']