如何根据表中同一行上其他元素的文本值获取元素的文本? [英] How to get text of an element on the basis of other element's text value on the same row in the table?

查看:14
本文介绍了如何根据表中同一行上其他元素的文本值获取元素的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在同一行上基于其他元素"SF Lead ID"找到元素分数"的文本.

I need to find the text of an element "score" on the basis of other element "SF Lead ID" on the same row.

"SF Lead ID"可以是动态的.例如我在这里使用硬编码值.

"SF Lead ID" can be dynamic. for example am using hard coded value here.

String sf_id = "00Q1l000003clVhEAI"; //this value is dynamic
String text= dvr.findElement(By.xpath("//*/tr/[contains(text(),'" + sf_id + "')]//*[@id='lead-score']")).getText();

请查看上图的html结构.帮我纠正xpath.

Please look the html structure on the above image. help me to correct the xpath.

当前其抛出以下错误-

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*/tr/[contains(text(),'00Q1l000003cldHEAQ')]//*[@id='lead-score'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*/tr/[contains(text(),'00Q1l000003cldHEAQ')]//*[@id='lead-score']' is not a valid XPath expression.
  (Session info: chrome=76.0.3809.132)
  (Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.12.6 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/invalid_selector_exception.html

推荐答案

要提取关于 SF Lead ID 分数,您必须诱使 WebDriverWait 作为 visibilityOfElementLocated(),您可以使用以下任一

To extract the score with respect to SF Lead ID you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • 使用 cssSelector getText():

String sf_id = "00Q1l000003clVhEAI";
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("tr[data-sf-lead-id='" + sf_id + "'] td#lead-score"))).getText());

  • 使用 xpath getAttribute():

    String sf_id = "00Q1l000003clVhEAI";
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//tr[@data-sf-lead-id='" + sf_id + "']//td[@id='lead-score']"))).getAttribute("innerHTML"));
    

  • 这篇关于如何根据表中同一行上其他元素的文本值获取元素的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆