Python Selenium - Checkbox Is Not Clicked With Explicit Wait - python

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

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

Javascript button wit no id not clicking with Selenium (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

Click a button to display a text box in python selenium

I am trying to click a button which un-hides another text box. The button click changes the script from
<span id="incident.u_brand_edit" style="display: none;">
to
<span id="incident.u_brand_edit" style>
Following is the button HTML
<button class="btn btn-default btn-ref" type="button" aria-labelledby="incident.u_brand_title_text"
data-target="#incident\.u_brand" title="" tabindex="0" id="incident.u_brand_unlock"
data-type="glide_list_unlock" data-ref="incident.u_brand" data-auto-close="false"
data-placement="auto" style="margin-right: 5px;" list-read-only="false"
data-original-title="Unlock Brand"><span class="icon icon-locked" aria-hidden="true"></span></button>
I am trying to achieve this using the following code
driver.find_element_by_id("incident.u_brand_unlock").click()
Also tried this
driver.find_element_by_id("incident.u_brand_unlock").send_keys("\n")
The above codes are focusing on the button but it's not clicking and the text box is not unhiding to perform further operations.
Try to use the ActionChains class in your code like below -
# Import
from selenium.webdriver import ActionChains
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)
Button_Click = wait.until(EC.element_to_be_clickable((By.ID, 'incident.u_brand_unlock')))
action.move_to_element(Button_Click).click().perform()
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, "button.btn.btn-default.btn-ref[id$='u_brand_unlock'][data-original-title='Unlock Brand']>span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-default btn-ref' and contains(#id, 'u_brand_unlock')][#data-original-title='Unlock Brand']/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
Found out the answer in this post . Had to remove the style attribute of the field I wanted to appear.

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

Unable to click on Button using Selenium Python

I'm trying to click on this button to go to the next page, but for some reason, I'm unable to. I tried xpath, css, and class selectors as well as the data-trekkie-id attribute, but nothing I have tried worked. Any help? Code below:
<div class="step__footer" data-step-footer="">
<button name="button" type="submit" class="step__footer__continue-btn btn " data-trekkie-id="continue_to_shipping_method_button" aria-busy="false">
<span class="btn__content">
Continue to shipping method
</span>
</button>
</div>
As per the HTML to invoke click() method on the button with text as Continue to shipping method you need to induce WebDriverWait and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.step__footer__continue-btn.btn[data-trekkie-id='continue_to_shipping_method_button']>span.btn__content"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='step__footer__continue-btn btn' and #data-trekkie-id='continue_to_shipping_method_button']/span[#class='btn__content']"))).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
This should work:
driver.find_element_by_xpath("//*[contains(local-name(), 'button') and contains(#class, 'step__footer__continue-btn')]").click()

Categories

Resources