Python 2.7 使用Selenium找不到正确的按钮ID

Python 2.7 使用Selenium找不到正确的按钮ID,python-2.7,selenium,Python 2.7,Selenium,我试图让Selenium在下面的参考URL中按下名为“剧集”的按钮。然而,我得到一个错误,说它找不到按钮ID我喂它 代码: 来自Chrome开发者工具的对象源代码: <button role="tab" type="button" id="tabs-id_2474-link-1" class="c-tabs__link" aria-selected="false" aria-controls="tabs-id_2474-article-1" data-test-id="episodes-t

我试图让Selenium在下面的参考URL中按下名为“剧集”的按钮。然而,我得到一个错误,说它找不到按钮ID我喂它

代码:

来自Chrome开发者工具的对象源代码:

<button role="tab" type="button" id="tabs-id_2474-link-1" class="c-tabs__link" aria-selected="false" aria-controls="tabs-id_2474-article-1" data-test-id="episodes-tab-button">Episodes</button>
剧集
错误:

Traceback (most recent call last):
  File "C:/Users/cg371/Downloads/Selenium)Test.py", line 8, in <module>
    class button_press():
  File "C:/Users/cg371/Downloads/Selenium)Test.py", line 12, in button_press
    button = driver.find_element_by_id('tabs-id_2474-link-1')
  File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "D:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="tabs-id_2474-link-1"]"}
  (Session info: chrome=78.0.3904.108)
回溯(最近一次呼叫最后一次):
文件“C:/Users/cg371/Downloads/Selenium)Test.py”,第8行,在
类按钮按()
文件“C:/Users/cg371/Downloads/Selenium)Test.py”,第12行,按按钮
button=驱动程序。按\u id(“tabs-id\u 2474-link-1”)查找\u元素\u
文件“D:\Python27\lib\site packages\selenium\webdriver\remote\webdriver.py”,第360行,按id查找元素
返回self.find_元素(by=by.ID,value=ID_u)
文件“D:\Python27\lib\site packages\selenium\webdriver\remote\webdriver.py”,第978行,位于find\u元素中
'value':value})['value']
文件“D:\Python27\lib\site packages\selenium\webdriver\remote\webdriver.py”,第321行,执行
self.error\u handler.check\u响应(响应)
文件“D:\Python27\lib\site packages\selenium\webdriver\remote\errorhandler.py”,第242行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“[id=“tabs-id_2474-link-1”]”
(会话信息:chrome=78.0.3904.108)
我做错了什么


谢谢

也许您可以尝试使用xpath


button=driver。通过xpath('/html/body/main/div/div/div/div/section/section/div[2]/div/div[2]/div/div/div[1]/div[1]/ul/li[2]/button)查找元素。

您可以尝试使用xpath吗


button=driver。通过xpath('/html/body/main/div/div/div/div/div/section/section/div[2]/div/div/div[1]/div[1]/ul/li[2]/button')查找元素。

尝试使用xpath,您会得到结果

button = driver.find_element_by_xpath("/html/body/main/div/div/div/div/section/section/div[2]/div/div[2]/div/div[1]/div[1]/ul/li[2]/button")

尝试使用xpath,您将得到结果

button = driver.find_element_by_xpath("/html/body/main/div/div/div/div/section/section/div[2]/div/div[2]/div/div[1]/div[1]/ul/li[2]/button")
试试这个:-

xpath:-

button = driver.find_element_by_xpath("//button[contains(.,'Episodes')]");
还可以尝试以下方法:-

button =driver.find_element_by_xpath("//li[2]/button");
为了找到该元素的正确路径,可以使用SeleniumIDE,它是mozilla中的扩展

查看下图:-

试试这个:-

xpath:-

button = driver.find_element_by_xpath("//button[contains(.,'Episodes')]");
还可以尝试以下方法:-

button =driver.find_element_by_xpath("//li[2]/button");
为了找到该元素的正确路径,可以使用SeleniumIDE,它是mozilla中的扩展

查看下图:-

提尔这个

This is xpath using button id. button = driver.find_element_by_xpath("//button[@id='tabs-id_2474-link-1']") 这是使用按钮id的xpath。 按钮=驱动程序。通过xpath(“按钮[@id='tabs-id\u 2474-link-1']”查找元素 泰尔:这个

This is xpath using button id. button = driver.find_element_by_xpath("//button[@id='tabs-id_2474-link-1']") 这是使用按钮id的xpath。 按钮=驱动程序。通过xpath(“按钮[@id='tabs-id\u 2474-link-1']”查找元素