Select CheckBox in Selenium Webdriver 2 - selenium

New to Selenium, trying to learn from online videos and discussion board.
I need to select a checkbox on the site, here is the HTML code.
<tr class="ms-test-List-Row" otypename="shopping" otype="10148" oid="{4D1A1C1B-DA33-414E-91D9-6202B0F71A6A}">
<td align="center" class="ms-test-List-NonDataCell">
<input tabindex="0" class="ms-test-RowCheckBox" id="checkBox_{4D1A1C1B-DA33-414E-91D9-6202B0F71A6A}" type="checkbox"/>
I tried using the:
dri.findElement(By.id("checkBox_{4D1A1C1B-DA33-414E-91D9-6202B0F71A6A}")).click();
but it's unable to locate this.
Any other method I should be using for this.
Appreciate your help!

As Vivek Singh suggested it was inside the iframe, I switched the frame and was able to find the checkbox element.
Thank you all for your time and suggestions! Really appreciate it.

Related

Python Selenium Click Link by href

There are a lot of methods I've tried for clicking on the link generated by the HTML bellow
<a class="btn btn--primary welcomePageButton" href="#/dispatchlist">
<span class="">View Dispatches/Invoices</span>
</a>
I've tried
driver.find_element_by_xpath("//a[contains(#href,'dispatchlist')]").click()
driver.find_element_by_link_text("View Dispatches/Invoices").click()
driver.find_element_by_partial_link_text("View Dispatches").click()
And in every case, I got NoSuchElementException.
Can you please guide me to solve this?
PS: I used the latest version of Google Chrome.
After trying a lot of xpaths, I decided to use JS snippets which worked like a charm
driver.execute_script("document.getElementsByClassName('btn')[0].click()")
However, thanks, everyone.

How to write dynamic xpath for img src

i am trying to automate couple of Selenium-TestNg scenarios from the website - http://ecommerce.saipratap.net/checkpersonaldetail.php
I was trying to click on the "continue "button, but it seems to be an image.
Below is the snippet of the code. How to write the xpath for the same?
tried using the xpath - //a[contains(#href,'checkoutshiping.php')], but it didnt work
<a href="checkoutshiping.php"> ==$0
<img src="images/continue.gif" border="0"
style="cursor:hand;"> ==$0
</a>
Are you sure the user is logged in? I see other set of tags when check the website.
Have you tried below xpath.
//*[.='Checkout']
Use - //a[#href = "checkoutshiping.ph"], its gonna find your button. I checked it on your website.
Here is example from your website

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

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