小编典典

selenium.common.exceptions.ElementNotVisibleException:消息:尝试使用Python +Selenium访问元素时元素不可见

selenium

我正在尝试在以下网站中输入用户名和密码:https :
//www.thegreatcoursesplus.com/sign-
in

driver = webdriver.Chrome()
driver.get('https://www.TheGreatCoursesPlus.com/sign-in')
driver.find_element_by_xpath('//h1[@class="sign-in-input"]').click()

这给出了以下异常:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

然后我尝试使用Java脚本:

driver.execute_script("document.getElementsByClassName('sign-in-input')[0].click()")
cmd = "document.getElementsByClassName('label-focus')[0].value = 'abc@abc.com'"
driver.execute_script(cmd)

没有错误,但没有文本发送到“电子邮件地址”字段。

有人可以指导我以正确的方式输入电子邮件地址,密码,然后单击“登录”。

感谢您的协助


阅读 306

收藏
2020-06-26

共1个答案

小编典典

此错误消息…

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

…表示所需的 元素WebDriver 实例尝试查找时在HTML
DOM中
不可见 。 __


ElementNotVisibleException

DOM
Tree
上存在某个元素时,会引发ElementNotVisibleException,但该元素不可见,因此无法与之交互。


原因

一个独到之处采取远离 ElementNotVisibleException 是事实 WebElement目前
的HTML内,并试图此异常时经常遇到click()或者read是从视图中隐藏的元素的属性。


作为 ElementNotVisibleException 确保 WebElement
HTML中,以便未来的解决方案将是两个折痕按照下面的步骤详情如下:

        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")
    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()

这个用例

您构造为的 xpath//h1[@class="sign-in-input"]与任何节点都不匹配。我们需要创建唯一的 xpath
来定位表示的元素Email AddressPassword并创建 WebDriverWaitSign In按钮。下面的代码块将帮助您实现相同的目标: __

    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("abc@abc.com")
    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()
2020-06-26