Unable to click on Button using Selenium Python - 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()

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 the ember.js enabled button using Selenium and Python

I have been trying to make this clickable and I just cannot understand what I am doing wrong.
I am also trying to induce webdriverwait, so that it is clicked when it appears.
This is my code so far:
def order(k):
driver = webdriver.Chrome(os.getcwd()+"\\webdriver\\chromedriver.exe")
driver.get("website.com/login-to-checkout")
driver.find_element_by_id('i0116').send_keys(k["email"])
driver.find_element_by_id('i0118').send_keys(k["password"])
driver.find_element_by_id('idSIButton9').click()
delay()
#sign in button
driver.find_element_by_id('idSIButton9').click()
#Button below I cant get to be clicked
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver, 7)
wait.until(presence_of_element_located((By.CSS_SELECTOR, "#ember1053")))
driver.find_element(By.id, "ember1053").click()
this is the source code for the button that I am trying to make clickable:
<div id="ember1037" class="btn-group m-b-lg m-t-lg order-call-to-action ember-view"><!----> <!--biBehavior 80 means place order Place Order-->
<button aria-live="polite" type="button" tabindex="0" data-m="{"aN":"shoppingCart","cN":"PlaceOrder","bhvr":80}" id="ember1053" class="btn theme-default btn-primary cli-purchase ember-view"><!----> Place order
</button></div>
The desired element is an Ember.js element and the value of the id attribute of the <button> will keep changing dynamically, every time you access the AUT(Application Under Test). Hence 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, "button.btn.theme-default.btn-primary.cli-purchase.ember-view[id^='ember'][type='button'][aria-live='polite']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn theme-default btn-primary cli-purchase ember-view' and starts-with(#id,'ember')][contains(., 'Place order') and #aria-live='polite']"))).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
References
You can find a couple of relevant detailed discussions in:
Selenium - Finding element based on ember
Ember dropdown selenium xpath
This may help but I've had issues with webdriver not clicking on a button when I use the id to find it.
The work around I've found is using the xpath instead of the id.
Like this, it's worth a try.
driver.find_element_by_xpath("""//*[#id="submit-button"]""").click()

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.

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