Having trouble clicking on a button/link with Selenium Webdriver - selenium

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

Related

Selenium RC: Need to click <td>

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

How to handle Pageload and display result in source tab using Selenium

Hi I need to implement a script that should capture all the object properties in a screen,
and prepare an xml for this i used javascript and jQuery.
Coming to my problem is if I click any submit button / link the page is moved to another
page i lost the previous page information, i am thinking to solve this using Selenium IDE
can any one please help me on this and i want to get the result in value field in the source
tab.
REQUIRED FORMAT in Source Tab
<tr>
<td>Testcase Name/ script Name</td>
<td>Method Name</td>
<td> XML as String </td>
</tr>
How can I insert my customize row in selenium IDE through javascript.
Thanks in advance.
What you can do with IDE is limited and it's not possible to do what you want using javascript.
I don't see IDE loosing its information when you move to other pages.

Shifting focus to a new popup window that has a null ID and name

I apologize if this question is redundant - I've seen a number of similar posts on this and other sites but none of them presented a solution that helped me.
I am using Selenium IDE to record a script that clicks a link (which opens a new window with a .shtml extension). I then need to switch focus to the new window and hit the save button to download a PDF.
I don't understand exactly how this works, but the url of the popup window is some generic url (https://www.theice.com/marketdata/reports/datawarehouse/ConsolidatedEndOfDayReportPDF.shtml) that hosts whatever report you generate on the previous page (https://www.theice.com/marketdata/reports/ReportCenter.shtml#report/142). If you try an openWindow command on the new URL it won't generate the report, you must open it through a link on the first page.
I want to refocus selenium to the new popup through selectWindow or selectPopup, but the issue is that the popup has no name, ID or title. Does anyone have a suggestion on how to avoid this issue?
Thanks,
Have not tried this out myself, but you could use this to find the window names and switch to it.
<tr>
<td>storeAllWindowNames</td>
<td>names</td>
<td></td>
</tr>
<tr>
<td>echo</td>
<td>${names}</td>
<td></td>
</tr>.
<tr>
<td>selectWindow</td>
<td><the name you find out></td>
<td></td>
</tr>.
You can try with storeAllWindowIds,storeAllWindowTitles. These three methods should return an array of windowids, something like this , bJHBGug2u8

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']

How to handle dynamic ID in HTML

I have to click on the button which has id. But this id is generated dynamically. And find By.className() is not doing anything.
The HTML code for the button:
<td class="x-btn-mc">
<em class="" unselectable="on">
<button id="cq-gen372" class=" x-btn-text" type="button">OK</button>
</em>
</td>
How to select the button and click on it in Java?
By.className() really was bugged in IE and some older Selenium versions. I didn't know it is still the case. Anyway! You can search by a lot of things, not just id:
You can try By.xpath("//button[text()='OK']"); if it is the only (or the first) OK button on page.
For more xpaths, see XPath v1.0 on w3.org and XPath v2.0 on w3.org - only for some new browsers!.
Or you can go with css selectors - The w3 again or wikipedia.
You can go with below options
//button[text()='OK']
xpath=//button[contains(., 'OK')]
//button[contains(#class, 'x-btn-text')]