gpt4 book ai didi

java - 如何根据表中同一行上其他元素的文本值获取元素的文本?

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

我需要根据同一行上的其他元素“SF Lead ID”查找元素“score”的文本。

“SF 潜在客户 ID”可以是动态的。例如,我在这里使用硬编码值。

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

enter image description here

请查看上图的 html 结构。帮我纠正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 相关的分数,您必须为 visibilityOfElementLocated() 引入WebDriverWait> 您可以使用以下任一 Locator Strategies :

  • 使用cssSelectorgetText():

    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());
  • 使用xpathgetAttribute():

    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"));

关于java - 如何根据表中同一行上其他元素的文本值获取元素的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57886216/

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