这个错误信息...
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
...意味着想要的元素不可见内HTML DOM https://www.w3schools.com/js/js_htmldom.asp而网络驱动程序实例试图找到它。
元素不可见异常
元素不可见异常 https://seleniumhq.github.io/selenium/docs/api/py/common/selenium.common.exceptions.html#selenium.common.exceptions.ElementNotVisibleException当元素存在于DOM Tree https://javascript.info/dom-nodes,但它不可见,因此无法与之交互。
Reason
一项积极的收获元素不可见异常事实是网页元素 is present在 HTML 中,尝试时通常会遇到此异常click()
or read
从视图中隐藏的元素的属性。
Solution
As 元素不可见异常确保网页元素 is present在 HTML 中,因此前面的解决方案将按照接下来的步骤进行两次折叠,如下详述:
-
如果您下一步是read所需元素的任何属性,那么您需要归纳WebDriver等待 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html#module-selenium.webdriver.support.wait和这个结合预期条件 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#module-selenium.webdriver.support.expected_conditions子句设置为元素位置可见性 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.visibility_of_element_located如下:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
my_value = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "element_xpath"))).get_attribute("innerHTML")
-
如果您下一步是调用click()
在所需的元素上,那么你需要诱导WebDriver等待 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html#module-selenium.webdriver.support.wait和这个结合预期条件 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#module-selenium.webdriver.support.expected_conditions子句设置为元素可点击 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.element_to_be_clickable如下:
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, "element_xpath"))).click()
这个用例
The xpath你构建为//h1[@class="sign-in-input"]
不匹配任何节点。我们需要创造独特的xpath定位代表的元素Email Address
, Password
and Sign In
按钮诱导WebDriver等待。下面的代码块将帮助您实现相同的目标:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.get('https://www.TheGreatCoursesPlus.com/sign-in')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='modal']//input[@name='email']"))).send_keys("[email protected] /cdn-cgi/l/email-protection")
driver.find_element_by_xpath("//div[@id='modal']//input[@name='password']").send_keys("password")
driver.find_element_by_xpath("//div[@id='modal']//button[@class='color-site sign-in-button']").click()