Java 如何使用链接文本从字段中提取WebElements并单击它
我想捕获字段中的文本,并能够单击该元素。当我使用以下命令时,它会将所有元素的文本提取到日志中:Java 如何使用链接文本从字段中提取WebElements并单击它,java,selenium,selenium-webdriver,extract,gettext,Java,Selenium,Selenium Webdriver,Extract,Gettext,我想捕获字段中的文本,并能够单击该元素。当我使用以下命令时,它会将所有元素的文本提取到日志中: String text; text = HomePageFields.TableOneColumn(driver).getText(); System.out.println("Table One Column contains following:\n" + text); TableOneColumnxpath位于不同的类上: public static WebElement TableOne
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链接。但其他文本包含在同一标记中,但在主标记中的位置不同
实际使用以下文本定位:-
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();
或
或者正如我在提供的屏幕截图中看到的,desire
具有id
属性,该属性的值看起来是唯一的。如果此元素的此属性值是固定的,则还可以尝试使用as:-
我建议也使用id为isc_3BL的div,但我不确定这是一个静态id。如果是,您肯定可以使用它与包含相同td的任何其他外部表隔离,其中包含文本“RFI概述”请共享页面的url。先生,您太棒了!它工作得很好。我还有一个问题,但与这个话题无关。我的动作课不起作用。我一直在寻找答案,但找不到。它抛出一个错误,提示“UnsupportedCommandException://…/moveto与已知命令不匹配。我不知道如何回答此问题。请用代码发布另一个问题。不要忘记包含导入,因为知道您是否正在使用写库很重要
WebElement el = driver.findElement(By.xpath(".//div[normalize-space(.) = 'RFI Overview']"));
System.out.println("Table One Column contains following:\n" + el.getText());
el.click();
WebElement el = driver.findElement(By.id("isc_3BL"));
System.out.println("Table One Column contains following:\n" + el.getText());
el.click();
driver.findElement(By.xpath("//td[.='RFI Overview']")).click();