没有这样的元素:无法在生产环境中使用chromedriver和Selenium定位元素 [英] no such element: Unable to locate element using chromedriver and Selenium in production environment

查看:145
本文介绍了没有这样的元素:无法在生产环境中使用chromedriver和Selenium定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的硒chromedriver有问题,我无法弄清楚是什么原因造成的.几周前,一切正常,突然开始出现此错误. 问题来自以下功能.

I have a problem with selenium chromedriver which I cannot figure out what's causing it. Some weeks ago everything was working OK, and suddenly this error started to show up. The problem is coming from the following function.

 def login_(browser):
    try:
        browser.get("some_url")
        # user credentials
        user = browser.find_element_by_xpath('//*[@id="username"]')
        user.send_keys(config('user'))
        password = browser.find_element_by_xpath('//*[@id="password"]')
        password.send_keys(config('pass'))
        login = browser.find_element_by_xpath('/html/body/div[1]/div/button')
        login.send_keys("\n")
        time.sleep(1)
        sidebar = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
        sidebar.send_keys("\n")
        app_submit = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/ul/li[1]/a')
        app_submit.send_keys("\n")
    except TimeoutException or NoSuchElementException:
        raise LoginException

此功能在开发环境(macOS 10.11)中正常工作,但在生产环境中引发以下错误:

This function works with no problem in the development environment (macOS 10.11), but throws the following error in the production environment:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="sidebar"]/ul/li[1]/a"}
(Session info: headless chrome=67.0.3396.79)
(Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee97XXX),platform=Linux 4.4.0-116-generic x86_64)

我已经在每个环境中同时更新了Chrome和chromedriver(分别为v67和2.40).我还给了它更多的time.sleep(15).但是问题仍然存在.我的最新猜测是,也许Webdriver的初始化无法正常工作:

I already updated both Chrome and chromedriver (v67 & 2.40, respectively) in each environment. I also gave it more time.sleep(15). But the problem persists. My latest guess is that maybe the initialization of the webdriver is not working properly:

def initiate_webdriver():
   option = webdriver.ChromeOptions()
   option.binary_location = config('GOOGLE_CHROME_BIN')
   option.add_argument('--disable-gpu')
   option.add_argument('window-size=1600,900')
   option.add_argument('--no-sandbox')
   if not config('DEBUG', cast=bool):
       display = Display(visible=0, size=(1600, 900))
       display.start()
       option.add_argument("--headless")
   else:
       option.add_argument("--incognito")
   return webdriver.Chrome(executable_path=config('CHROMEDRIVER_PATH'), chrome_options=option)

因为,如果Display不起作用,则可能没有提到的sidebar,而是其他按钮.

Because, if the Display is not working, then there may not be the mentioned sidebar but some other button.

所以我的问题是:有人遇到过类似的问题吗?有没有办法知道驾驶员正在寻找这样的元素时页面显示什么?

So my questions are: does anybody have had a similar issue? Is there a way to know what is the page showing at the time the driver is looking for such an element?

推荐答案

根据login_(browser)方法的几件事:

  • 您已经通过以下方式确定了 Login 按钮:

login = browser.find_element_by_xpath('/html/body/div[1]/div/button')

我建议您调用send_keys("\n")来使用 onclick() 事件通过login.click()模拟点击登录"按钮,如下所示:

I would suggest rather invoking send_keys("\n") take help of the onclick() event through login.click() to mock the clicking of Login button as follows:

login = browser.find_element_by_xpath('/html/body/div[1]/div/button')
login.click()

  • 接下来,当您确定侧边栏诱导 WebDriverWait 使元素可点击时,如下所示:

  • Next when you identify the sidebar induce WebDriverWait for the element to be clickable as follows:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="sidebar"]/ul/li[1]/a'))).click()
    

  • 您提到您的代码块在 macOS 10.11 环境中可以完美运行,但在生产环境(Linux)中引发以下错误不同的浏览器在不同的OS体系结构中以不同的方式呈现 HTML DOM .因此,必须使用 relative xpath 代替 absolute xpath ,如下所示:

  • As you mentioned your code code block works perfect in macOS 10.11 environment but throws the following error in the production environment (Linux) it is highly possible that different browsers renders the HTML DOM differently in different OS architecture. So instead of absolute xpath you must use relative xpath as follows:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@attribute='value']"))).click()
    

  • 根据initiate_webdriver()方法的几件事:

    • 按照> Headless Chrome入门 参数--disable-gpu仅适用于 Windows ,而不适用于 Linux OS 的有效配置.所以需要删除:

    • As per Getting Started with Headless Chrome the argument --disable-gpu is applicable only for Windows but not a valid configuration for Linux OS. So need o remove:

    option.add_argument('--disable-gpu')
    

    注意:您必须添加以下导入:

    Note : You have to add the following imports :

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

    这篇关于没有这样的元素:无法在生产环境中使用chromedriver和Selenium定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆