简体   繁体   English

Selenium WebDriver HTML 代码

[英]Selenium WebDriver HTML Code

This is what the error displays when I use the xpath这是我使用 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.

相关问题 如何通过与Java一起使用Selenium WebDriver从此HTML代码中获取文本 - How to get Text from this HTML code By using Selenium WebDriver with Java 使用 selenium webdriver 和 HTML 代码解包到 select 日历 - unbale to select date using selenium webdriver and HTML code is there for calendar Selenium WebDriver-编译HTML字符串 - Selenium WebDriver - compile HTML string 如何在使用 Selenium WebDriver 自动化时处理 HTML 源代码中动态更改的类名 - How to deal with Dynamically changing class names in HTML source code while automating using Selenium WebDriver 如何在Selenium Webdriver中使用Java验证HTML代码中是否存在用于下拉列表的标记 - How to verify whether a tag is present in the html code for a drop down using java in Selenium Webdriver 使用Java在Selenium WebDriver(Selenium 2)中使用selenium.refresh()的等效代码 - Equivalent code of selenium.refresh() in Selenium WebDriver (Selenium 2) using Java 将带有 html/Javascript 的字符串放入 selenium webdriver - put a string with html/Javascript into selenium webdriver 使用Selenium Webdriver解析html表时出现问题 - Problematic parsing of html table with selenium webdriver Selenium Webdriver-输入js / html元素标识 - Selenium Webdriver - Input js/html elements identification 使用Java的Selenium WebDriver和HTML Window位置 - Selenium WebDriver and HTML Window location by using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM