Selenium 元素确实存在时的硒元素异常
我正在开发一个discord机器人,它使用selenium从instagram帐户获取缩放链接。它一直工作得很好,直到今天才开始给出错误Selenium 元素确实存在时的硒元素异常,selenium,selenium-webdriver,selenium-chromedriver,python-3.8,Selenium,Selenium Webdriver,Selenium Chromedriver,Python 3.8,我正在开发一个discord机器人,它使用selenium从instagram帐户获取缩放链接。它一直工作得很好,直到今天才开始给出错误selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:“/*[@id=“react root”]/section/main/div/header/section/div[2]/a[1]}。我不知道这是为什么。我已经检查了Xpath是
selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:“/*[@id=“react root”]/section/main/div/header/section/div[2]/a[1]}
。我不知道这是为什么。我已经检查了Xpath是否正确,并添加了一个sleep函数以允许它有时间加载。到目前为止,唯一有效的方法就是将它使用的浏览器从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的双引号中使用转义字符 尝试 而不是
//*[@id="react-root"]/section/main/div/header/section/div[2]/a[1]
好的,我刚试过,但没有改变任何事情。我仍然得到错误。另外,我的代码在大约一个月内工作正常,但昨天就停止工作了,所以我看不出这会有什么变化。它以前工作过,因为我对字符串使用了单引号,所以react根周围的双引号没有结束字符串。好的,我只是尝试了一下,但它没有改变任何东西。我仍然得到错误。另外,我的代码在大约一个月内工作正常,但昨天就停止工作了,所以我看不出这会有什么变化。它以前是有效的,因为我对字符串使用单引号,所以react根周围的双引号不会结束字符串。