Click in Selenium + Python - python

I have a problem with click in Selenium, it doesn't click on the button. This is my code:
from selenium import webdriver
import time
import click
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://help.instagram.com/contact/723586364339719/")
submit_button = driver.find_element_by_xpath('//*[#id="u_0_8"]')
submit_button.click()
This is the HTML code:
<button value="1" class="_42ft _4jy0 _4jy4 _4jy1 selected _51sy" type="submit" id="u_0_8">Enviar</button>

You can do it with using explicit wait for the button:
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, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='u_0_8']"))).click()
I hope it helps you!

Instead of this:
submit_button = driver.find_element_by_xpath('//*[#id="u_0_8"]')
submit_button.click()
Try this:
driver.find_element_by_id("id='u_0_8']").click()

The desired element is a dynamic element, so 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:
driver.get('https://help.instagram.com/contact/723586364339719/')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
Using XPATH:
driver.get('https://help.instagram.com/contact/723586364339719/')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[#value='1' and normalize-space()='Send']"))).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
Browser Snapshot:

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

How can I use selenium to click a button

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

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

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

selenium.common.exceptions.TimeoutException: this error will getting when I'm trying to button click using python

I'm using Selenium package to button click for a website. When I'm trying i'm getting an error as:
selenium.common.exceptions.TimeoutException: Message:
this is the code which trying to run.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as BSoup
from datetime import date, timedelta
import pyodbc
import datetime
browser = webdriver.Firefox()
browser.get("https://www.cbsl.gov.lk/rates-and-indicators/exchange-rates/daily-buy-and-sell-exchange-rates")
#time.sleep(10)
#browser.find_element_by_xpath('//*[#id="dailyexchange"]/div[2]/div/button[1]').click()
wait = WebDriverWait(browser, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="dailyexchange"]/div[2]/div/button[1]')))
element.click()
You need to switch to iframe before clicking button:
browser.get("https://www.cbsl.gov.lk/rates-and-indicators/exchange-rates/daily-buy-and-sell-exchange-rates")
wait = WebDriverWait(browser, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it('iFrameResizer2'))
element = wait.until(EC.element_to_be_clickable((By.NAME, 'select_button')))
element.location_once_scrolled_into_view
element.click()
The desired element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following solutions:
Using CSS_SELECTOR:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
browser.get("https://www.cbsl.gov.lk/rates-and-indicators/exchange-rates/daily-buy-and-sell-exchange-rates")
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iFrameResizer2[src='/cbsl_custom/exratestt/exratestt.php']")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-default[name='select_button']"))).click()
Browser Snapshot:

Categories