gpt4 book ai didi

java - 如何使用字段中的 linkText 提取 WebElements 并单击它

转载 作者:行者123 更新时间:2023-11-30 10:37:26 26 4
gpt4 key购买 nike

我想在字段中捕获文本并能够单击该元素。当我使用以下内容时,它会将所有元素的文本提取到日志中:

 String text;
text = HomePageFields.TableOneColumn(driver).getText();
System.out.println("Table One Column contains following:\n" + text);

TableOneColumn xpath 在不同的类上:

public static WebElement TableOneColumn(WebDriver driver) throws IOException {
element = driver.findElement(By.xpath("//div[contains(@eventproxy,'isc_QMetricsView_0')]/div[1]/div[1]/div[1]/div[1]/div[1]/div[contains(@style,'position')]/div"));
return element;

我尝试使用:

HomePageFields.TableOneColumn(driver).findElement(By.linkText("RFI Overview")).click();

但它给出了一个错误,说找不到该元素。

这是该特定文本的 html 链接。但是其他文本包含在同一个标​​签中,但在该主标签中的不同位置。

HTML path

最佳答案

实际上 By.linkText() 定位<a>元素显示的确切文本,而 desire 文本不在任何 <a> 中标记。这就是你遇到麻烦的原因。

您应该尝试使用 By.xpath() 此文本如下:-

WebElement el = driver.findElement(By.xpath(".//div[descendant::td[text() = 'RFI Overview']]"));
System.out.println("Table One Column contains following:\n" + el.getText());
el.click();

或者

WebElement el = driver.findElement(By.xpath(".//div[normalize-space(.) = 'RFI Overview']"));
System.out.println("Table One Column contains following:\n" + el.getText());
el.click();

或者正如我在提供的屏幕截图中看到的那样 <div>id值看起来唯一的属性。如果此元素的此属性值是固定的,您也可以尝试使用 By.id() 作为:-

WebElement el = driver.findElement(By.id("isc_3BL"));
System.out.println("Table One Column contains following:\n" + el.getText());
el.click();

关于java - 如何使用字段中的 linkText 提取 WebElements 并单击它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40119869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com