wait for element and click - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: wait for element and click (/thread-25139.html) |
wait for element and click - windows11 - Mar-21-2020 Hello world, I need some help . I´m try to search and did´t find the solution or it was to confuse. I'm trying to wait for the element to be visible in the website to open, right now I have a time, but some times the internet is slow and I can´t open. I would like to have something like "wait until the element appear and click()" thank you this is my code: from selenium import webdriver browser=webdriver.Firefox(executable_path="C:\\Users\\Sabrina\\Desktop\\geckodriver-v0.26.0-win64\\geckodriver.exe") browser.get("website link, for exemple www.youtube.com") import time time.sleep(8) browser.find_element_by_xpath("/html/body/div[2]/main/div/div[2]/section[1]").click() import time time.sleep(8) browser.find_element_by_xpath("/html/body/div[2]/main/div[2]/section[1]").click() RE: wait for element and click - Larz60+ - Mar-21-2020 1st only import packages once, usually at the start of the program You need to learn more about scraping. Please do the following tutorials: web scraping part 1 web scraping part 2 RE: wait for element and click - windows11 - Mar-21-2020 Found it how I do. If some one have the same question is here the solution 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 def init_driver(): driver=webdriver.Firefox(executable_path="C:\\Users\\SAbrina\\Desktop\\geckodriver-v0.26.0-win64\\geckodriver.exe") driver.wait=WebDriverWait(driver, 5) return driver def get_data(driver): driver.get("website link") element=driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div[2]/main/div/div[2]/section[1]"))) driver.find_element_by_xpath("/html/body/div[2]/main/div/div[2]/section[1]").click() driver=init_driver() get_data(driver) |