英文:
Selenium chromedriver unable to locate href tag nested inside class tags
问题
我正在使用 selenium
网页驱动程序,我想捕获或点击这个 HTML 标签:
\<a class="sub-nav-link style-scope app-shell active" href="#/trend-analysis/tag-search"\>
我尝试使用以下方法:
Driver.findElement(By.xpath(//a[@href='#/trend-analysis/tag-search']")).click();
<div class="collapse navbar-collapse style-scope app-shell" id="navbarNav">
<ul class="app-nav navbar-nav mr-auto style-scope app-shell">
<li class="nav-item style-scope app-shell">
<a class="nav-link p-3 px-4 style-scope app-shell" href="/#/dashboard">Dashboard</a>
</li>
<li class="nav-item style-scope app-shell">
<a class="nav-link py-3 px-4 style-scope app-shell active" href="/#/trend-analysis/tag-search">Trend
Analysis</a>
<ul class="sub-nav d-flex flex-row flex-nowrap list-unstyled style-scope app-shell">
<li class="sub-nav-item style-scope app-shell">
<a class="sub-nav-link style-scope app-shell active" href="#/trend-analysis/tag-search">
Trend Analysis
</a>
错误信息
无法定位元素:{"method":"xpath","selector":"//*[@href='/#/trend-analysis/tag-search']"}
我的猜测是它无法检测到此 href,因为它深埋在许多 <a>
和 <class>
标签之下。请指导我。
新错误
没有这样的元素:无法定位元素:{"method":"xpath","selector":"//li[@class='nav-item style-scope app-shell']/a[contains(@href,'#/trend-analysis/tag-search')]"}
使用以下代码:
driver.findElement(By.xpath("//li[@class='nav-item style-scope app-shell']/a[contains(@href,'#/trend-analysis/tag-search')]")).click();
英文:
I'm using selenium
web-driver, I want to capture or click on
this HTML tag as
\<a class="sub-nav-link style-scope app-shell active" href="#/trend-analysis/tag-search"\>
I tried using
Driver.findElement(By.xpath(//a[@href='#/trend-analysis/tag-search']")).click();
<div class="collapse navbar-collapse style-scope app-shell" id="navbarNav">
<ul class="app-nav navbar-nav mr-auto style-scope app-shell">
<li class="nav-item style-scope app-shell">
<a class="nav-link p-3 px-4 style-scope app-shell" href="/#/dashboard">Dashboard</a>
</li>
<li class="nav-item style-scope app-shell">
<a class="nav-link py-3 px-4 style-scope app-shell active" href="/#/trend-analysis/tag-search">Trend
Analysis</a>
<ul class="sub-nav d-flex flex-row flex-nowrap list-unstyled style-scope app-shell">
<li class="sub-nav-item style-scope app-shell">
<a class="sub-nav-link style-scope app-shell active" href="#/trend-analysis/tag-search">
Trend Analysis
</a>
error
Unable to locate element: {"method":"xpath","selector":"//*[@href='/#/trend-analysis/tag-search']"}
my guess is that it doesn't detect this href because its buried deep under many <a>
and <class>
tags. Please guide me.
new error
no such element: Unable to locate element: {"method":"xpath","selector":"//li[@class='nav-item style-scope app-shell']/a[contains(@href,'#/trend-analysis/tag-search')]"}
with
driver.findElement(By.xpath("//li[@class='nav-item style-scope app-shell']/a[contains(@href,'#/trend-analysis/tag-search')]")).click();
答案1
得分: 1
实际上,在你的回答中,你没有第一个"是这个问题吗????"
因为这个:
driver.findElement(By.xpath("//a[@href='#/trend-analysis/tag-search']")).click();
似乎运行良好。
如果问题是 "#",就像下面的人说的,也许这样可以修复:
driver.findElement(By.xpath("//li[@class='nav-item style-scope app-shell']/a[contains(@href,'#/trend-analysis/tag-search')]")).click();
希望我有帮助。
英文:
Actually in your answer you dont have the first "is this the problem????
because this:
driver.findElement(By.xpath("//a[@href='#/trend-analysis/tag-search']")).click();
seems to be working well
if the problem is the # as the guy below sais maybe this will fix it:
driver.findElement(By.xpath("//li[@class='nav-item style-scope app-shell']/a[contains(@href,'#/trend-analysis/tag-search')]")).click();
WISH I HELPED
答案2
得分: 0
这可能被视为NAA(不是答案),但我没有足够的声望来发表评论... 你可以删除它,我不会生气。
我每天解析# URL's
。如果你查看下面的Stack Overflow回答,你会看到关于在Selenium中使用#
在**URL
**内的一些内容。
注意: URL
中**#
** 的典型用途是创建一个自我引用链接,这意味着**URL
** 通常指向与当前查看的页面完全相同的**URL
**,但确切地说是页面上的不同位置。
如果你在代码中使用**Selenium
的X-Path
语句来查找要定位的URL
或HREF
,并且这个URL
中既有#
,又有更多的正斜杠/dirs...
字符,根据这个网站,会调用Java-Script(AJAX),也许Selenium
没有像你的X-Path
语句认为的那样看到HREF
**的样子。
链接网站的重要摘录:
> 当访问http://mywebsite.com/#/page1时,它会执行一个ajax
> 请求到我们传统的url http://mywebsite.com/page1 获取该页面的HTML,并将页面的内容加载到我们现有的页面中。
长话短说:你的**X-Path
中有一个#
井号,这意味着它是一种特殊类型的URL
,可能需要一种特殊类型的X-Path
**...(这是我实际上无法提供的答案,因为我不知道 :))
英文:
This probably qualifies as NAA (Not an answer), but I don't have the reputation to post comments... You can delete it, I won't be offended.
I parse # URL's
everyday. If you will look at the following Stack Overflow answer, you will see a little bit about using the #
inside of a URL
with Selenium.
NOTE: The typical purpose of a #
in a URL
is to make a self-referential-link which means that the URL
usually points to the exact same URL
as the page you are currently looking at - but, precisely, to a different location on the page.
If the URL
or HREF
you are trying to located using the X-Path
statement in your code using Selenium
has both a #
and more forward-slash /dirs...
characters in it, according to this web-site, Java-Script (AJAX) is being invoked, and perhaps Selenium
is not seeing the HREF
the way your X-Path
statement thinks it is supposed to look like.
Salient excerpt from linked site:
> When http://mywebsite.com/#/page1 is accessed it will perform an ajax
> request to our traditional url http://mywebsite.com/page1 fetch the
> HTML of that page, and load in the page's content into our existing
> page.
Long story short: You X-Path
has a #
pound-sign in it, which means it is a 'special-type' of URL
which might need a special-type of X-Path
... (which is an answer I actually can't provide, cuz I don't know
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论