Selenium NoSuchElementException 当元素确实存在时



我正在开发一个不和谐的机器人,它使用硒从Instagram帐户中获取缩放链接。它一直运行良好,直到今天它开始给出错误selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="react-root"]/section/main/div/header/section/div[2]/a[1]"}.我不知道这是为什么。我已经检查了 Xpath 是否正确,并添加了一个睡眠功能以允许它有时间加载。到目前为止,唯一有效的方法是将它使用的浏览器从 Chrome 更改为 Firefox,但是这是在我的本地机器上完成的,无法在实际代码中完成,因为我已将其部署到 heroku 并运行无头 chrome 浏览器,我不想经历试图让 Firefox 工作的麻烦,因为我花了一个多小时才获得 chrome加工。

以下是相关代码:

@client.command()
async def zoom(ctx):
await ctx.send("Retrieving current zoom link from instagram...nPlease wait...")
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)
url = "https://instagram.com/profile/"
driver.get(url)    # load instagram page
time.sleep(5)
link = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[2]/a[1]').get_attribute("innerHTML")  # Get zoom link
await ctx.send("Here is the current zoom link:nhttps://" + link)
driver.quit()

以下是整个错误回溯:

2020-06-02T03:45:55.540096+00:00 app[worker.1]: Traceback (most recent call last):
2020-06-02T03:45:55.540174+00:00 app[worker.1]: File "main.py", line 48, in zoom
2020-06-02T03:45:55.540177+00:00 app[worker.1]: link = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[2]/a[1]').get_attribute("innerHTML")  # Get zoom link
2020-06-02T03:45:55.540204+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
2020-06-02T03:45:55.540206+00:00 app[worker.1]: return self.find_element(by=By.XPATH, value=xpath)
2020-06-02T03:45:55.540264+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
2020-06-02T03:45:55.540265+00:00 app[worker.1]: 'value': value})['value']
2020-06-02T03:45:55.540305+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
2020-06-02T03:45:55.540305+00:00 app[worker.1]: self.error_handler.check_response(response)
2020-06-02T03:45:55.540306+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
2020-06-02T03:45:55.540306+00:00 app[worker.1]: raise exception_class(message, screen, stacktrace)
2020-06-02T03:45:55.540375+00:00 app[worker.1]: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="react-root"]/section/main/div/header/section/div[2]/a[1]"}
2020-06-02T03:45:55.540377+00:00 app[worker.1]: (Session info: headless chrome=83.0.4103.61)

非常感谢任何帮助,我真的需要让这个机器人恢复并再次运行。 提前感谢!

您没有在 id 的双引号中使用转义字符;

尝试;

//*[@id="react-root"]/section/main/div/header/section/div[2]/a[1]

而不是;

//*[@id="react-root"]/section/main/div/header/section/div[2]/a[1]

相关内容

  • 没有找到相关文章

最新更新