How can I use selenium to click a button - python

HTML snapshot of website:
The button is the rebounds button on 'https://app.prizepicks.com/board'. I've tried using the copy xPath feature, but that does not work.

Try to wait until the elements show up or use time.sleep()
Below xpath works for me
rebounds_btn = driver.find_element_by_xpath('//*[contains(text(), "Rebounds")]')
rebounds_btn.click()

Try the below,
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Rebounds')]"))).click()
do not forget to import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

To click on the element with text as Rebounds you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following solution:
driver.get("https://app.prizepicks.com/board")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Rebounds']"))).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

Close popups in Oddschecker using Selenium Python

Need to close the cookies and ad popup on this page, I know you can do it using webdriverwait and either CSS_SELECTOR or X tags but I can't find the specific tags for the buttons, as when you click 'inspect' they disappear.
Code trials:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
s=Service('C:\Program Files (x86)\chromedriver.exe')
driver = webdriver.Chrome(service=s)
driver.get(https://www.oddschecker.com/)
Any help greatly appreciated.
To click on the element OK you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get("https://www.oddschecker.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='CookieBannerAcceptButton']"))).click()
Using XPATH:
driver.get("https://www.oddschecker.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='OK']"))).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

selenium create a delay before selecting button by name

I would like to target a button that contains the name ="loginButton", and click the element after loading etc so it doesnt click on the loading spinner. How do I go about this webdriverwait targetting the name element?
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((find_element_by_name('loginButton'), "loginButton"))).click()
You are almost correct.
For the provided example try using following code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.NAME, "loginButton")))
element.click();
See here for more explanations

Trying to click a button but keep getting an error with xpath

I am trying to automate pulling credit charges into an excel sheet; I have managed to get the login working. Once I enter the website, there's a button titled "Search". I cant seem to figure out how to have that button clicked.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import time
chromedriver = "C:/Python_Ex/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
delay = 30
driver.get("https://global.americanexpress.com/activity/date-range?from=2020-05-01&to=2020-05-30")
driver.find_element_by_xpath('//*[#id="eliloUserID"]').send_keys("removed")
driver.find_element_by_xpath('//*[#id="eliloPassword"]').send_keys("removed")
driver.find_element_by_xpath('//*[#id="loginSubmit"]').click()
time.sleep(10)
#print(driver.find_elements_by_xpath('//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button'))
search_button = driver.find_elements_by_xpath('//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button')
search_button.click()
html tag is as follows
<button class="btn btn-fluid" tabindex="0" type="button"> <span>Search</span></button>
Xpath as follows
//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button
Any help is appreciated.
To click() on the <button> with text as Search using Selenium 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, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-fluid[type='button']>span"))).click()
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-fluid']/span[text()='Search']"))).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
After login submited, try to add the below code:
...
...
driver.find_element_by_xpath('//*[#id="loginSubmit"]').click()
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-fluid']//span[text()='Search']"))).click()
Following import:
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 follow button on Instagram?

I am trying to click the follow button on Instagram using Python Selenium
https://www.instagram.com/luvly_zuby/?hl=en
I've tried the bellow code but it's not working.
#click follow
follow = driver.find_element_by_partial_link_text("Follo")
ActionChains(driver).move_to_element(follow).click().perform()
You can click on the element by using simple selenium click by finding the element using its text in the xpath and then using explicit wait on the element.
You can do it like:
follow_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//button[text()='Follow']")))
follow_button.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
This should work for you. Pass the WebElement to click(element) method.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('testUrl')
follow = driver.find_element(By.XPATH, '//button[contains(text(),'Follow')]')
webdriver.ActionChains(driver).move_to_element(follow).click(follow).perform()
Try to find the element using this css selector a.BY3EC > button and wait using .element_to_be_clickable:
follow = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.BY3EC > button')))
follow.click()
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Selenium Can't find element when using find_element through Python

Keep getting unable to locate element error message.
The first find element by xpath was fine, but the second one is giving me a hard time.
Here is my code:
import XLUtils
from selenium import webdriver
driver=webdriver.Chrome(executable_path="C:\Chrome_Driver\Chromedriver.exe")
driver.get("https://www.canada.ca/en/revenue-agency/services/e-services/e-services-businesses/payroll-deductions-online-calculator.html")
driver.find_element_by_xpath('/html/body/main/div[1]/div[7]/p/a[1]').click()
driver.find_element_by_xpath('//*[#id="welcome_button_next"]').click()
You'll need to wait a bit for the Next button to appear.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
wait = WebDriverWait(driver, 10)
e = wait.until(
EC.presence_of_element_located((By.XPATH, '//*[#id="welcome_button_next"]'))
)
e.click()
To click on the element with text as Next you need to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.btn.btn-primary#welcome_button_next"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='btn btn-primary' and #id="welcome_button_next"]"))).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

Categories