简体   繁体   English

使用Java + Selenium WebDriver获取一些文本

[英]Get some text with java + selenium WebDriver

I want to get only the text "Invitation sent to xxxxx" , I do not want to get what's inside the 我只想收到“邀请发送到xxxxx”文本,我不想得到里面的内容

<button class="action" data-ember-action="" data-ember-action-3995="3995">
          Visualizar perfil
 </button>

I'm getting the text this way: 我这样得到文本:

String pessoapopu = driver.findElement(By.className("artdeco-toast-message")).getText();                
System.out.println(pessoapopu); 

Page structure: 页面结构:

<div class="artdeco-toast-inner" data-ember-action="" data-ember-action-3994="3994">
    <li-icon aria-hidden="true" type="success-pebble-icon" class="artdeco-toast-icon"><svg viewBox="0 0 24 24" width="24px" height="24px" x="0" y="0" preserveAspectRatio="xMinYMin meet" class="artdeco-icon"><g class="large-icon" style="fill: currentColor">
        <g id="success-pebble-icon">
          <g>
            <circle class="circle" r="9.1" stroke="currentColor" stroke-width="1.8" cx="12" cy="12" fill="none" transform="rotate(-90 12 12)"></circle>
            <path d="M15.667,8L17,9.042l-5.316,7.36c-0.297,0.395-0.739,0.594-1.184,0.599c-0.455,0.005-0.911-0.195-1.215-0.599l-2.441-3.456l1.416-1.028l2.227,3.167L15.667,8z" fill="currentColor"></path>
            <rect style="fill:none;" width="24" height="24"></rect>
          </g>
        </g>
        <g id="Layer_1">
        </g>
      </g></svg></li-icon>
    <p class="artdeco-toast-message">
              Invitation sent to xxxxx
        <button class="action" data-ember-action="" data-ember-action-3995="3995">
          Visualizar perfil
        </button>

    </p>
  </div>

图片

You can use //p[@class='artdeco-toast-message']/text() xpath to locate the Invitation sent to xxxxx text but selenium doesn't support text() method in xpath to locate a text node. 您可以使用//p[@class='artdeco-toast-message']/text() xpath来定位Invitation sent to xxxxx文本的Invitation sent to xxxxx但是硒不支持xpath中的text()方法来定位文本节点。

Either if you try to locate the element using below xpath to exclude the button text by using not() function of xpath : 如果您尝试使用xpath下方的元素来定位元素,以通过使用xpath的not()函数来排除按钮文本:

//p[@class='artdeco-toast-message']/node()[not(self::button)]

Again it locating the element using text node so Selenium doesn't allow this 再次使用文本节点定位元素,因此Selenium不允许这样做

Here one solution available to execute same xpath ie JavascriptExecutor 这里有一个解决方案可以执行相同的xpath,即JavascriptExecutor

Use evaluate() method of JavaScript and evaluate your xpath using JavascriptExecutor 使用JavaScript的validate evaluate()方法并使用JavascriptExecutor评估您的xpath

JavascriptExecutor js = (JavascriptExecutor)driver;
Object message = js.executeScript("var value = document.evaluate(\"//p[@class='artdeco-toast-message']/text()\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");
System.out.println(message.toString().trim());

OR 要么

JavascriptExecutor js = (JavascriptExecutor)driver;
Object message = js.executeScript("var value = document.evaluate(\"//p[@class='artdeco-toast-message']/node()[not(self::button)]\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");
System.out.println(message.toString().trim());

It will give you the expected result. 它会给您预期的结果。 No need to get all data and then formatting using String functions. 无需获取所有数据,然后使用String函数进行格式化。

You can explore evaluate() in detail from here 您可以从这里详细探索evaluate()

You can try with the following code. 您可以尝试以下代码。

WebElement text=driver.findElement(By.className("artdeco-toast-message"));
String wholeText = text.getText();
String unWantedText=text.findElement(By.className("action")).getText();
String RequiredText=wholeText.replace(unWantedText,"");
System.out.println(RequiredText);

In fact Selenium's getText() method retrieves all the text in sub elements too, so the approach, recommended by Murthi is the most applicable, as far as I know. 实际上,Selenium的getText()方法也可以检索子元素中的所有文本,因此据我所知, Murthi推荐的方法是最适用的。 Try this approach: 试试这个方法:

String newLine = System.getProperty("line.separator");
String pessoapopu = driver.findElement(By.className("artdeco-toast-message"))
                           .getText().replaceAll(newLine, "");

Or try/ask for more convenient HTML code. 尝试/询问更方便的HTML代码。

给定HTML,您应该能够.getText() ,在换行符上拆分该文本,然后进行第一次拆分以获取所需的文本。

driver.findElement(By.cssSelector("p.artdeco-toast-message")).getText().split("\r\n")[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java中的Selenium Webdriver获取文本 - Get Text with Selenium Webdriver in Java 使用Java中的Selenium Webdriver获取格式化的文本 - Get formatted text with Selenium Webdriver in Java 如何使用 Java 在 Selenium WebDriver 的隐藏字段中输入一些文本 - How to type some text in hidden field in Selenium WebDriver using Java Java Selenium Webdriver:是否有某种形式的隐式等待get,如果不是,为什么呢? - Java Selenium Webdriver: Is there some form of get for implicitWait and if not why not? Java Selenium Webdriver-从文本字段中获取文本 - Java selenium webdriver - get text out of a text field Selenium Webdriver-Java-从Gmail获取特定文本 - Selenium Webdriver - Java - Get a specific text from Gmail 如何通过与Java一起使用Selenium WebDriver从此HTML代码中获取文本 - How to get Text from this HTML code By using Selenium WebDriver with Java 如何使用Selenium WebDriver Java从标签内部获取文本? - How to get text from inside tag use selenium webdriver java? 是否可以使用Java使用Selenium WebDriver从验证码图像中获取文本? - Is it possible to get text from captcha image with Selenium WebDriver using Java? Java/Selenium WebDriver:如何获取用户输入并将其插入到文本字段中? - Java/Selenium WebDriver: How to get user input and insert it in to a text field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM