我正在尝试从Seeking Alpha下载文章。由于许多文章仅是订阅者专有的,因此我必须先登录才能查看和下载这些文章。我使用的代码如下。我的问题是click()
没有任何作用。手动登录后,效果很好。
# open website
url_lib = 'https://seekingalpha.com'
driver.get(url_lib)
# sign in
sign_in = driver.find_element_by_id('sign-in')
sign_in.click()
# id and pw
sign_id = driver.find_element_by_id('authentication_login_email')
sign_id.send_keys(Email address)
time.sleep(2)
sign_pw = driver.find_element_by_id('authentication_login_password')
sign_pw.send_keys(Password)
time.sleep(2)
# click submit button
submit_button = driver.find_element_by_id('log-btn')
submit_button.click()
任何建议将不胜感激!预先感谢!
答案 0 :(得分:1)
所需元素是JavaScript启用的元素,因此您必须为所需element_to_be_clickable
引入 WebDriverWait ,并且可以使用以下解决方案:
代码块:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
# options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://seekingalpha.com")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#sign-in"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control#authentication_login_email"))).send_keys("AnnieQW")
driver.find_element_by_css_selector("input.form-control#authentication_login_password").send_keys("AnnieQW")
driver.find_element_by_css_selector("input.orange-btn[value='Sign in']").click()
浏览器快照: