【问题标题】:Unable to extract the text using gettext in Selenium WebDriver and also unable to click it无法在 Selenium WebDriver 中使用 gettext 提取文本,也无法单击它
【发布时间】:2013-05-18 07:05:49
【问题描述】:

我在 Selenium WebDriver 中找不到以下代码的 gettext

<a id="551" class="blueTextNormal1 spc" onclick="sPh(this,'079');return false;" title="079">Country</a>

我想获取 Country 的值。我尝试使用xpath

driver.findElement(By.xpath("//*[@id='551']").getText())

但它没有返回任何值。当我尝试使用

driver.findElement(By.xpath("//*[@id='551']")).getAttribute("title"))

我得到的值为“079”。

我该如何继续?

【问题讨论】:

标签: java selenium-webdriver


【解决方案1】:

这也取决于代码。试试下面的代码:

请使用getAttribute("innerHTML") 而不是getText(),这将返回您要查找的内容,包括任何不可见的HTML。

<div class="no-docs-selected">
    <p class="icon-alert">Please select a doc</p>
</div>

我一直在寻找Please select a doc,但我没有找到getText()。但下面的工作。

driver.findElement(By.xpath("//p[@class='icon-alert']")).getAttribute("innerHTML");

【讨论】:

  • 嗨@joe我是selenium的新手,无论你建议什么都对我有用,但我没有任何澄清,即何时使用getAttribute(“atrb”)以及何时使用获取文本()?我试图搜索原因,但找不到任何具体结果。
【解决方案2】:

我遇到了同样的问题,然后我将 .getText() 更新为 .getAttribute("textContent") 并且它起作用了。

【讨论】:

  • 属性名"textContent"从何而来?是字面意思吗?
  • 这应该是该字段的 HTML 中的一个属性。
【解决方案3】:

你能得到属性标题,而不是文本,真是令人惊讶。

试试

driver.findelement(By.xpath("//a[@id='551' and contains(text(),'Country')]")).isDisplayed();

driver.findelement(By.xpath("//a[@id='551' and text()='Country']")).isDisplayed();

【讨论】:

  • 嗨,阿伦,感谢回复 [link](driver.findElement(By.xpath("//a[@id='551' and contains(text(),'Country')]" )).isDisplayed());) iam 得到 False 我认为因为 onclick="sPh(this,'079') 我们得到“假”,如果 iam 尝试 driver.findElement(By.xpath("//a [@id='551']")).click() 出错。org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与命令持续时间交互
  • 大家好,这是redbus.in的网站,我想获取每个城市的电话号码。当我们点击每个城市时,电话号码将出现在后面。例如。艾哈迈达巴德、班加罗尔等。希望这有助于理解应用程序。
【解决方案4】:

我还有一个案例,getAttribute("innerHTML") 有效,但 getText() 无效。原来该元素存在,但不可见或不显示。

当我在调用 getText() 之前先滚动到元素时,它起作用了。

    if(!element.isDisplayed()) 
        scrollIntoView(element);
    element.getText();

scrollIntoView函数是这样的:

    public void scrollIntoView(WebElement element) {
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    }

因此我不需要使用 getAttribute("innerHTML")`。

【讨论】:

    【解决方案5】:

    这对我有用:

    System.out.println(driver.findElement(By.id("error")).getAttribute("textContent"));
    

    代码:

    driver.get("https://login.salesforce.com/");
    driver.findElement(By.id("username")).sendKeys("Hello");
    driver.findElement(By.id("password")).sendKeys("Hello");
    driver.findElement(By.id("Login")).click();
    System.out.println(driver.findElement(By.id("error")).getAttribute("textContent"));
    driver.close();
    

    【讨论】:

      【解决方案6】:

      除非在复制到 StackOverflow 时出现拼写错误,否则您在 getText 之前缺少括号。改变 driver.findElement(By.xpath("//*[@id='551']").getText())driver.findElement(By.xpath("//*[@id='551']")).getText()

      我尝试了以下代码,它非常适合我。

      WebDriver driver = new ChromeDriver();
      driver.get("C:\\test.html");
      System.out.println(driver.findElement(By.xpath("//*[@id='551']")).getText());
      System.out.println(driver.findElement(By.xpath("//*@id='551']")).getAttribute("title"));
      

      【讨论】:

      • 谢谢@buddha,我已经测试过了,只有 getattribute 将值返回为 079 而不是 getText();我已经使用 By.xpath("//a[@id='551' and text()='Country']")).isDisplayed();它返回 false
      • 最后我能够解决元素被隐藏的问题,所以我使用WebElement labelNode = driver.findElement(By.xpath("//*[@id='551']")); String labelNodeText = (String) ((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML",labelNode); 能够返回所需的艾哈迈达巴德。感谢所有帮助过我的人。
      【解决方案7】:
      String text1 = driver.findElementByXPath("//input[@value='EFASTGTC']/../ancestor::tr[1]/scipt[1]").getAttribute("innerText"); 
              
      System.out.println("Value=" + text1);
      

      如果您希望在脚本标记之间写入文本,此代码将起作用。

      【讨论】:

        【解决方案8】:

        使用&lt;WebElement&gt;.getAttribute("value") 提取文本。

        【讨论】:

          【解决方案9】:

          在类似的情况下,这对我有用:

          .getAttribute("innerHTML");
          .getAttribute("innerText");
          .getAttribute("outerText");
          

          【讨论】:

          • 这几乎不是一个答案。你怎么称呼这些方法?一个网络元素?如果是这样,你如何找到它?另请注意,属性与元素的文本内容不同。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-06
          • 1970-01-01
          • 2016-08-03
          • 1970-01-01
          相关资源
          最近更新 更多