I copied the xPath of a certain element I am trying to click and obtained the following:
我复制了我想要点击的某个元素的xPath,并获得了以下内容:
//*[@id="ctl00_ctl00_ctl00_body_homebody_PageMainContent_ResultsGrid_ctl00__0"]/td[1]/a
My code then contains the following:
然后我的代码包含以下内容:
driver.find_element_by_id("//*[@id="ctl00_ctl00_ctl00_body_homebody_PageMainContent_ResultsGrid_ctl00__0"]/td[1]/a").click()
Any idea why I might be getting this error: r aise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
知道为什么我可能会收到此错误:r aise exception_class(message,screen,stacktrace)selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素
1 个解决方案
#1
1
XPATHs may contain id
(or many other HTML tags), but that does not mean that the id
is the XPATH. Use:
XPATH可能包含id(或许多其他HTML标记),但这并不意味着id是XPATH。使用:
driver.find_element_by_xpath('//*[@id="ctl00_ctl00_ctl00_body_homebody_PageMainContent_ResultsGrid_ctl00__0"]/td[1]/a').click()
and it should work - assuming there is a valid XPATH at that location on your website.
它应该工作 - 假设您的网站上的该位置有一个有效的XPATH。
also, re: your comment on the OP - //*
specifies relative XPATH vs absolute XPATH - essentially meaning that it skips the initial and tags and goes straight to the middle of the DOM.
另外,你对OP的评论 - // *指定相对XPATH与绝对XPATH - 本质上意味着它跳过了初始和标签并直接进入DOM的中间。
#1
1
XPATHs may contain id
(or many other HTML tags), but that does not mean that the id
is the XPATH. Use:
XPATH可能包含id(或许多其他HTML标记),但这并不意味着id是XPATH。使用:
driver.find_element_by_xpath('//*[@id="ctl00_ctl00_ctl00_body_homebody_PageMainContent_ResultsGrid_ctl00__0"]/td[1]/a').click()
and it should work - assuming there is a valid XPATH at that location on your website.
它应该工作 - 假设您的网站上的该位置有一个有效的XPATH。
also, re: your comment on the OP - //*
specifies relative XPATH vs absolute XPATH - essentially meaning that it skips the initial and tags and goes straight to the middle of the DOM.
另外,你对OP的评论 - // *指定相对XPATH与绝对XPATH - 本质上意味着它跳过了初始和标签并直接进入DOM的中间。