Javascript button wit no id not clicking with Selenium (Python) - python

I am trying to click on the following button using Selenium in Python. However, any way I have tried to approach clicking on that button has failed and I was hoping someone could help. For all intents and purposes. I am a novice in both Python/Selenium so any help would be greatly appreciated!
<tr>
<td colspan=3></td>
<td align=center valign='center'> <a href="javascript:myFunction('/mailbox/jsp/MBIList.jsp')"
onMouseOver="chgButton('Go','GoOver'); window.status=''; return true;"
onMouseOut="chgButton('Go','GoOff'); window.status=''; return true;"
onMouseDown="chgButton('Go','GoDown'); return true;"><img border=0
src="/mailbox/images/go_off.gif" vspace=7 name='Go' align='top'></a>
</td>
</tr>

U can click on any element using any of the element attributes. For example in this case I would use attribute #name='Go' in your Xpath, where you are trying to select element.

The desired element is a dynamic element, so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='/mailbox/jsp/MBIList'] > img[name='Go'][src^='/mailbox/images/go_off']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href, '/mailbox/jsp/MBIList')]/img[#name='Go' and starts-with(#src, '/mailbox/images/go_off')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

Selenium: How to click on a link with no class or ID

How do I click this link using Selenium in Python? I've been able to get to the webpage using Selenium and click other links that have IDs. But this one doesn't have an ID or name.
HTML:
<div ng-repeat="dashboard in $ctrl.dashboards track by $index" class="btn btn-primary ng-scope active" ng-class="{active: dashboard.Dashboard_Id == $ctrl.activeDashboard.Dashboard_Id}" ng-click="$ctrl.dashboardClick(dashboard)" style="">
<span ng-bind="dashboard.Name" class="ng-binding">Hours By Activity</span>
</div>
I've tried this with no luck:
driver.find_elements_by_xpath("//*[contains(text(), 'Hours by Activity')]")
The desired element is an Angular element so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.ng-binding[ng-bind='dashboard.Name']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='ng-binding' and #ng-bind='dashboard.Name'][text()='Hours By Activity']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Python Selenium - Checkbox Is Not Clicked With Explicit Wait

H, I am trying to click on the below checkbox.
<thead class="ant-table-thead" xpath="1">
<tr>
<th class="ant-table-selection-column"><span class="ant-table-header-column"><div><span class="ant-table-column-title"><div class="ant-table-selection"><label class="ant-checkbox-wrapper"><span class="ant-checkbox"><input type="checkbox" class="ant-checkbox-input" value=""><span class="ant-checkbox-inner"></span></span>
</label>
</div>
</span><span class="ant-table-column-sorter"></span></div>
</span>
</th>
</tr>
</thead>
It gets clicked instantly only as follows:
Checkbox = "//thead/tr[1]/th[1]/span[1]/div[1]/span[1]/div[1]/label[1]/span[1]/input[1]"
driver.find_element_by_xpath(Checkbox).click()
However, when I try to add explicit wait, the checkbox doesn't get clicked
if WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, Checkbox))): driver.find_element_by_xpath(
Checkbox).click()
I have also tried EC.visibility_of_element_located but also doesn't work
Thanks for the help in advance
looking at your code and at the documentation i think i figured out, i cannot reproduce to test but i will try to explain why isn't working:
From the doc:
from selenium.common.exceptions import TimeoutException
try:
element = WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH, Checkbox))
driver.find_element_by_xpath(Checkbox).click()
except TimeoutException as ex :
*put here the "else" code, it will be execute if the wait ends with no Checkbox found so the WebDriver will Throw a TimeoutException*
To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "thead.ant-table-thead input.ant-checkbox-input"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//thead[#class='ant-table-thead']//input[#class='ant-checkbox-input']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to click on GWT enabled elements using Selenium and Python

I'm working with Selenium as a newbie and also as a newbie developer.
I try to solve this XPath but with no results that is why I'm looking for help.
So I want to click in the checkbox which has a dynamic id but it is not that easy to find this checkbox based on tittle="Viewers"
Please notice there is a whole list of checkboxes with names on right from a checkbox in div which I want to include in my tests.
HTML:
<span class="row-selection"><input type="checkbox" value="on" id="gwt-uid-2215" tabindex="0"><label for="gwt-uid-2215"></label></span> <div class="row-label" title="Viewers">Viewers</div>
Snapshot;
The desired element is a GWT enabled element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following xpath based Locator Strategy:
Using xpath based on title attribute:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#title='Viewers']//preceding::span[1]//label"))).click()
Using xpath based on innerHTML:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Viewers']//preceding::span[1]//label"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Find a <td> element using Selenium Python

I'm new to python and I'm trying to write a web scraping script. I'm trying to double click on this element (it's neither a button nor link - just a td element) and am having trouble with even finding it in the first place.
Below is the code
<td align="left" valign="middle" class=" "
title="Business Profile (Imported)">Business Profile (Imported)</td>
When I select it, the class changes. I suspect this is where the problem is at
<td align="left" valign="middle" class=" cellselected "
title="Business Profile (Imported)">Business Profile (Imported)</td>
I used css selector & xpath. None works. I tried both:
driver.find_element_by_xpath('//td[#title="Business Profile (Imported)"]').click()
driver.find_element_by_css_selector("td[title='Business Profile (Imported)']")
This is the error I get:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//td[#title="Business Profile (Imported)"]"}
Any help would be tremendously appreciated. Thanks!!
To locate the desired <td> element you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
XPATH 1:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[contains(#title, 'Imported') and starts-with(., 'Business Profile')]")))
XPATH 2:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[starts-with(#title, 'Business Profile') and contains(., 'Imported')]")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
To handle dynamic element induce WebDriverWait and element_to_be_clickable and use the following xpath
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//td[#title='Business Profile (Imported)' and text()='Business Profile (Imported)']"))).click()
OR
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//td[#title='Business Profile (Imported)'][contains(.,'Business Profile (Imported)')]"))).click()
You need to imports following to execute above code.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

How to click the button as per the html using Selenium and Python

I am learning Selenium and trying to click the GO button:
https://speedtest.telstra.com/
<button class="button background-primary-hover text-primary" aria-label="start your speedtest">
<span style="font-size: unset; overflow-wrap: unset;">GO</span></button>
What are all possible Selenium methods to get that button clicked,
elem = driver.find_element_by_....???
I also would like to see what I found, so should print(elem.text) then be used?
As per the website https://speedtest.telstra.com/ the desired element is within an <iframe> so you need to induce WebDriverWait to switch to the <iframe> and then look out for the element and you can use the following solution:
Using XPATH:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='speed-test' and #src='//telstra-nbn.speedtestcustom.com']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='button background-primary-hover text-primary']/span[contains(.,'GO')]"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"//iframe.speed-test[src*='speedtestcustom']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button.background-primary-hover.text-primary[aria-label='start your speedtest']>span"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You have to use xpath, there is xpath helper tool for chrome. You can install it.
button = driver.find_element_by_xpath("your xpath")
button.click()
Try this:
browser.find_element_by_class_name("button background-primary-hover text-primary").click()
Since it will select the element and click it.

Categories

Resources