Python Selenium is_displayed() 返回 true 并且仍然引发 ElementNotVisible 异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17432638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:11:57  来源:igfitidea点击:

Python Selenium is_displayed() returns true and ElementNotVisible exception is still raised?

pythonselenium

提问by InstallGentoo

Before calling any of the element's send_keys(), I first check if it's enabled and visible so it doesn't raise an exception.

在调用任何元素的 send_keys() 之前,我首先检查它是否已启用和可见,因此它不会引发异常。

What happens is is_Displayed returns True and when I try to send_keys to that element it still raises an exception of ElementNotVisible. Is this some form of a bug?

发生的事情是 is_Displayed 返回 True,当我尝试将 send_keys 发送到该元素时,它仍然引发 ElementNotVisible 异常。这是某种形式的错误吗?

It works on some websites, it doesn't work on another.

它适用于某些网站,但不适用于其他网站。

def login():
 elem = browser.find_elements_by_xpath('//input[contains(@name, "user")]')
 for elements in elem:
  if elements.is_displayed():
   if elements.is_enabled():
    elements.send_keys(username)
    elem = browser.find_elements_by_xpath('//input[contains(@name, "pass")]')
    for elements in elem:
     if elements.is_displayed():
       if elements.is_enabled():
        elements.clear()
        elements.send_keys(password + Keys.RETURN)   #Crashes here
        time.sleep(4)
        return

回答by Belrog

Try this:

尝试这个:

def login():
 user_elements = browser.find_elements_by_xpath('//input[contains(@name, "user")]')
 for user in user_elements:
  if user.is_displayed():
   if user.is_enabled():
    user.send_keys(username)
    pass_elements = browser.find_elements_by_xpath('//input[contains(@name, "pass")]')
    for passw in pass_elements:
     if passw.is_displayed():
       if passw.is_enabled():
        passw.clear()
        passw.send_keys(password + Keys.RETURN)   #Crashes here
        time.sleep(4)
        return

It's likely your choice of variable names make you clobber the outside loop with the inside loop.

您对变量名称的选择可能会使您用内部循环破坏外部循环。

回答by InstallGentoo

If anyone is still wondering what problem was, it was caused by javascript hiding element after page was fully loaded.

如果有人仍然想知道是什么问题,这是由页面完全加载后 javascript 隐藏元素引起的。

Completely disabling javascript on page solved that issue.

在页面上完全禁用 javascript 解决了这个问题。

回答by big-vl

pls use my code, my github https://github.com/big-vl/isdisplayed_selenium/blob/master/isDisplayed.py

请使用我的代码,我的 github https://github.com/big-vl/isdisplayed_selenium/blob/master/isDisplayed.py

def isDisplayed():
    try:
        browser.find_element_by_xpath("//*[text()='find text vwhis in page']")
    except NoSuchElementException:
        return False
    return True

    #use function

if (isDisplayed() == True):
    print('text find, pleas replace hash tag or replace xpatch')
else:
    print('not found text, my style php/python *smile*')