I've been trying to get Python to login to a website and download a csv file (one after the other because they can take a long time, and they can't be downloaded in parallel). I can't click SENECA01 button because of ElementClickInterceptedException, so after reading stackoverflow I tried using an explicit wait.
Here's the error:
AttributeError: module 'selenium.webdriver' has no attribute 'find_element'
Here's the code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from getpass import getpass
from selenium.webdriver.common.by import By
import time
opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
opt.add_argument("--disable-extensions")
# Pass the argument 1 to allow and 2 to block
opt.add_experimental_option("prefs", { \
"profile.default_content_setting_values.media_stream_mic": 1,
"profile.default_content_setting_values.media_stream_camera": 1,
"profile.default_content_setting_values.geolocation": 1,
"profile.default_content_setting_values.notifications": 1
})
PATH = ("C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python39\\chromedriver.exe")
driver = webdriver.Chrome(PATH, chrome_options=opt)
wait = WebDriverWait(webdriver, 5)
driver.get("https://xxxx.xxx-xxx.org/logon/login")
print(driver.title)
username = input("Enter in your username: ")
password = getpass("Enter your password: ")
username_textbox = driver.find_element_by_id("username")
username_textbox.send_keys(username)
password_textbox = driver.find_element_by_id("password")
password_textbox.send_keys(password)
login_button = driver.find_element_by_class_name("formstylebut")
login_button.submit()
link = driver.find_element_by_link_text("Big Data (CAD)")
link.click()
link = driver.find_element_by_link_text("Run a Report")
link.click()
link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "SENECA01")))
link = driver.find_element_by_link_text("SENECA01")
link.click()
CodePudding user response:
This
wait = WebDriverWait(webdriver, 5)
should be
wait = WebDriverWait(driver, 5)
and this is how you should use this
link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "SENECA01")))
trust it helps!
CodePudding user response:
To start using Selenium first and foremost you need:
from selenium import webdriver
Additionally, as per the documentation the WebDriverWait is defined as follows:
class selenium.webdriver.support.wait.WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
Where the Constructor, takes a WebDriver instance and timeout in seconds.
As your webdriver instance is:
driver = webdriver.Chrome(PATH, chrome_options=opt)
So instead of webdriver
you need to pass the same driver
instance to the WebDriverWait() as follows:
wait = WebDriverWait(driver, 5)