Can Python selenium be used to click on webpage labels? - selenium

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

Related

Xpath for dynamic select menus

I'm trying to automate some web testing using Kantu and Selenium. The page is using pq-select ParamQuery to generate select menus, but neither Kantu or Selenium can see them in the page.
I'm guessing my best bet is to use xpath to locate them, but I'm not too sure on how to do that. The HTML for the select menu is:
<td style="white-space: nowrap " aria-describedby="df230254-d8a5-4ba1-9950-58d26145d5a9" role="gridcell" data-container-for="section1" id="sectiongrid_active_cell" class="">
If I could use the data-container-for that would be the best I think, but I'm not sure how to get that via xpath.
As per the HTML you have shared you can use either of the following solutions:
XPath - 1:
//td[#id='sectiongrid_active_cell' and #data-container-for='section1']
XPath - 2:
//td[#id='sectiongrid_active_cell' and #data-container-for='section1'][#role='gridcell']
Note: The element is a ParamQuery select element which is being converted to theme ready jQueryUI widget, you have to induce WebDriverWait for the element to be visible/interactable.

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 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 Write Selenium Xpath

I am stuck here. I am trying a write Xpath for the following. I am trying to click "Browse". The ID is changing every time.
I have tried this as my Xpath:
//*[#id='ext-gen43']/em/span/span
<a id="ext-gen43" class="x-tab-right" href="#">
<em class="x-tab-left">
<span class="x-tab-strip-inner">
<span class="x-tab-strip-text ">Browse</span>
</span>
</em>
</a>
XPATH WRITING PLUGINS :
Inorder to write xpath's by yourself you must first install firebug
and firepath plugins, they are the plugins available for Firefox
browser.
You can also install xpath checker which is another plugin available for firefox. It's a awesome plugin because you can actually
see the UI elements in the xpath checker as you are writing the
xpath.
EXAMPLE :
Inorder to write xpath's by yourself you must following the child and parent hierarchy available in the HTML tab in firebug.
STEPS :
Hover on the element you want to write xpath for using the small blue arrow available in the firebug toolbar in the top left corner.
Now click on the element, you would observe that the tag for the respective element in the firebug is highlighted.
Say your tag looks like :-
<label> class="control-label col-sm-3" for="name"> Your Name <label>
So the xpath for respective tag would be :-
//label[#class="control-label col-sm-3"]
So the above xpath specifies //parent tag which is label and into the bracket we should specify the locator, locator can be id, class, name anything.
And in your case the xpath would be :-
//span[#class="x-tab-strip-text"]
Have You tried copy xpath for given element in your browser(check element -> copy xpath)?
Then delete id and check once again.
It should be easy if you know how to select element by xpath with given class.
Try to use firepath to get xpath. It`s addon for FireFox https://addons.mozilla.org/uk/firefox/addon/firepath/

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.