how to find button to click in Selenium using python? - python

</div>
<fieldset class="pi-search-form__footer">
<div class="pi-search-form__footer-cta" data-ng-show="stay.isStandardBooking()">
<button class="btn btn--primary" data-ng-click="submitSearch()">
<span data-ng-switch="ctaText">
<span data-ng-switch-when="checkavailability">Check availability</span>
<span data-ng-switch-when="searchnow">Search now</span>
<span data-ng-switch-default data-pi-track-click="SEARCH_AGAIN">Search</span>
</span>
</button>
</div>
This is my button code and I want to click "Check availability" button.
I use that code but it not works.
browser.find_elements_by_xpath(".//span[contains(text(), 'Check availability')]")

Here is the Answer to your Question:
As you want to click on a button so your xpath should be unique and should identify a single element. Hence instead of find_elements_by_xpath you should consider using find_element_by_xpath. To click on the Check availability button you can use the following line of code:
//ensure the imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
//other code
//click on Check availability button
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn--primary']//span[#data-ng-switch-when='checkavailability']")))
browser.find_element_by_xpath("//button[#class='btn btn--primary']//span[#data-ng-switch-when='checkavailability']").click()
Let me know if this Answers your Question.

it should be
browser.find_elements_by_xpath(".//span[contains(text(), 'Check availability')]")
and if it's not working use explicit wait.
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 30)
checkAvailability = wait.until(EC.presence_of_element_located((By.XPATH, ".//span[contains(text(), 'Check availability')]")))

Related

How to Select Element Based on Parameters Passed in Button Onclick Method

I want to be able to select the button that has the timestamp i am targeting. Here is the HTML:
<a class="mdc-button mdc-button--unelevated mdc-ripple-upgraded" href="#" onclick="selectTime(7160, null, "2021-08-23T08:15:00-04:00", '8b96beed5b0b8f7b34e51853f14ef6eb'); return false;" style="--mdc-ripple-fg-size:55px; --mdc-ripple-fg-scale:1.97805; --mdc-ripple-fg-translate-start:17px, -19.5px; --mdc-ripple-fg-translate-end:18.5px, -9.5px;">
<div class="mdc-button__ripple"></div>
<span class="mdc-button__label">8:15 AM</span>
</a>
I do not know how to target the timestamp parameter it is calling in onclick that can be used to select the button. I have tried to find element by targeting the label's text, but I cannot click on the label, with it giving me this error :
Message: element click intercepted: Element <span class="mdc-button__label">...</span> is not clickable at point (132, 380). Other element would receive the click: <div class="mdc-button__ripple"></div>
Any help would be greatly appreciated.
You can use the below css_selector :
a[class$='mdc-ripple-upgraded'] span[class='mdc-button__label']
use it like this :
wait = WebDriverWait(driver, 10)
ActionChains(driver).move_to_element(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[class$='mdc-ripple-upgraded'] span[class='mdc-button__label']")))).click().perform()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

Not able to target 2nd matching element by XPATH

I have a html content like this
<div class="ng-star-inserted">
<span class="ng-star-inserted">
Seismic interpreter
</span>
<span class="ng-star-inserted">
Geophysicist
</span>
</div>
I have a selenium script where I am trying to get the first role name and second role name.
I tried this, but it's not giving me the 1st and 2nd elements as expected. Any idea what am I missing? thanks!
'//div[#class="ng-star-inserted"]//span//a[1]'
'//div[#class="ng-star-inserted"]//span//a[2]'
You can xpath indexing in this case :
so instead of this :
'//div[#class="ng-star-inserted"]//span//a[1]'
'//div[#class="ng-star-inserted"]//span//a[2]'
use this :
"(//div[#class='ng-star-inserted']//span//a)[1]"
"(//div[#class='ng-star-inserted']//span//a)[2]"
or if you think the text won't change, you can make use of LINK_TEXT as well or PARTIAL_LINK_TEXT
Sample code :-
wait = WebDriverWait(driver, 10)
button1 = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Seismic interpreter")))
button1.click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
So you can use the below way
((//a[contains(text(),'Seismic interpreter')])[2])
((//a[contains(text(),'Geophysicist')])[2])
clickLink=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"((//a[contains(text(),'Seismic interpreter')])[2])")))
checkbox.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.

Select check box using Python and Selenuim

It would be nice if someone knows how to select the checkbox using Selenium with Python.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
no_class = browser.find_element(By.XPATH, "//div[#id='icon-literary-collections']/following-sibling::a[1]")
no_class.click
and this is the HTML Part :
<div class="a-checkbox" style="">
<label for="checkbox-non--classifiable" style="">
<input id="checkbox-non--classifiable" type="checkbox" name="" value="" nodeid="non--classifiable" style="" class="">
<i class="a-icon a-icon-checkbox"></i>
<span class="a-label a-checkbox-label" style="">Non-Classifiable</span>
</label>
</div>
I want to select the checkbox "Non-classifiable" but i couldn't do that it tried I tried as id, name, link_text but could not detect what should be used?
You can try clicking on it by using its text in the xpath and using explicit wait so that the script waits till the element is present on the page.
You can do it like:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Non-Classifiable']")))
element.click()
You need 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
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[#class='a-label a-checkbox-label']"))).click()
Javascript click:
checkBox=WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[#class='a-label a-checkbox-label']")))
driver.execute_script("arguments[0].click();", checkBox)
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

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