从模态窗口看不到元素 - Selenium Python

时间:2017-07-12 18:31:31

标签: python selenium modal-dialog

我正在尝试登录网页以便我可以从中抓取数据,但每次尝试时,都会收到错误元素不可见的错误。我试图点击的元素是模态窗口内的按钮,但该窗口的html仅在单击登录按钮后出现。我可以点击登录按钮,但是我无法点击之后弹出的教师按钮。有什么想法吗?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

login_url = 'https://edpuzzle.com/'
username = '***'
password = '***'
ChromeDriver = r'C:\Users\admin\Python\chromedriver.exe'

driver = webdriver.Chrome(ChromeDriver)
driver.get(login_url)
driver.find_element_by_css_selector('button.btn.btn-default-
transparent').click()
driver.find_element_by_css_selector('p.modal-title.text-lg.text-center.edp-
role-title')
driver.find_element_by_css_selector('button.btn.white-btn.btn-lg.btn-
block.big-btn.edp-teacher-role').click()

1 个答案:

答案 0 :(得分:1)

您尝试在实际可见之前单击按钮。你需要等一段时间:

from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver.get(login_url)
driver.find_element_by_id('edp-login').click()
wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn.white-btn.btn-lg.btn-block.big-btn.edp-teacher-role"))).click()