[英]Selenium WebDriver HTML Code
This is what the error displays when I use the xpath这是我使用 xpath 时显示的错误
What command on the selenium webdriver would I use to click a text which has a xpath of我将使用 selenium webdriver 上的什么命令来单击 xpath 为
html/body/div[1]/div/div/div[2]/div[2]/div[2]/a
I tried using我尝试使用
driver.findElement(By.xpath("html/body/div[1]/div/div/div[2]/div[2]/div[2]/a")).click();
but still doesn't work... any suggestions?但仍然不起作用......有什么建议吗?
Your xpath is incorrect.您的 xpath 不正确。 You are trying to hit an absolute path with html/body/div[1]/div/div/div[2]/div[2]/div[2]/a
.您正在尝试使用html/body/div[1]/div/div/div[2]/div[2]/div[2]/a
绝对路径。 You are missing a /
so instead it should be /html/body/div[1]/div/div/div[2]/div[2]/div[2]/a
你缺少一个/
所以它应该是/html/body/div[1]/div/div/div[2]/div[2]/div[2]/a
try:尝试:
driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div[2]/a")).click();
Instead of using absolute xpath which is not reliable way to identify an element, you can try this: //input[@id='partnerId']/following-sibling::a
.您可以尝试以下操作: //input[@id='partnerId']/following-sibling::a
而不是使用不可靠的绝对 xpath 方法来识别元素。 It finds an element with unique id and then locates nearest sibling having tag a
它找到一个具有唯一 id 的元素,然后找到最近的具有标记a
兄弟元素
I have seen similar issues when the element to be located was in a separate iframe.当要定位的元素位于单独的 iframe 中时,我看到了类似的问题。
Using FirePath or source html, check if there are any iframes and if yes, use使用 FirePath 或源 html,检查是否有任何 iframe,如果有,请使用
driver.switchTo().frame("framename") and then use driver.switchTo().frame("framename") 然后使用
driver.findElement(By.xpath("/html/body/div 1 /div/div/div[2]/div[2]/div[2]/a")).click(); driver.findElement(By.xpath("/html/body/div 1 /div/div/div[2]/div[2]/div[2]/a")).click();
What about just using By.linkText()
or By.partialLinkText()
instead of XPath?仅使用By.linkText()
或By.partialLinkText()
而不是 XPath 怎么样? Using XPath in this way is "easy" but very fragile I would recommend against it.以这种方式使用 XPath 是“容易”但非常脆弱的,我建议不要使用它。
driver.findElement(By.partialLinkText("EARN up to "));
You can, of course, edit the partial link text to be more or less specific depending on what you are looking for and how unique the text is on the page.当然,您可以根据您要查找的内容以及文本在页面上的独特程度,将部分链接文本编辑为或多或少具体。
Since that didn't work, let's try a different approach.既然这不起作用,让我们尝试不同的方法。 Let's find all the links in the table you have pictured above.让我们在上图中的表格中找到所有链接。 The code below will grab the A tags in the table and then click the first one.下面的代码将抓取表格中的 A 标签,然后单击第一个。 My guess from the picture is that the text contained in the link will likely change so having a more flexible way to find the links is better than using the link text.我从图片中猜测链接中包含的文本可能会发生变化,因此使用更灵活的方式来查找链接比使用链接文本更好。 See if the below works.看看下面是否有效。
List<WebElement> links = driver.findElements(By.cssSelector("div.data-block-sub-popular-offers > div.row-heading > a"));
links[0].click();
只需使用下面的xpath:
driver.findElement(By.xpath("//div[@class='row-40percent']/a[contains(text(),'Earn upto 2.5%')]").click();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.