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

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

问题描述

我对 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("
")
        time.sleep(1)
        sidebar = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
        sidebar.send_keys("
")
        app_submit = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/ul/li[1]/a')
        app_submit.send_keys("
")
    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?

推荐答案

提供登录后报元素未找到错误,所以我认为登录失败,页面重定向到某个地方.您可以使用屏幕截图选项对页面进行屏幕截图,然后查看驱动程序加载的页面.

It's report that the element not found error after you supplying the login , so I think the login failed and the page redirected to somewhere. You can use screenshot option to take a screenshot of the page and then see which page the driver load.

driver.save_screenshot("path to save screen.jpeg")

您还可以保存原始 html 代码并检查同一页面.

Also you can save the raw html code and inspect the same page.

网络驱动程序截图

在 Python 中使用 Selenium 将网页保存在火狐

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

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