Java 使用 selenium webdriver 打印所需的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36555887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:24:56  来源:igfitidea点击:

Printing desired text using selenium webdriver

javaseleniumselenium-webdriver

提问by Roy

Below is the HTML Query.

下面是 HTML 查询。

<div id="dvCount" style="">
  <span>Total Log Count : </span>
  <span id="spnLogCount">46</span>
</div>

I want to print the value 46in Selenium WebDriver. Please let me know the code.

我想46在 Selenium WebDriver 中打印值。请让我知道代码。

I am using the following code but I am unable to get the value:

我正在使用以下代码,但无法获取该值:

WebElement Text= driver.Findelement(By.cssselector(xpath).gettext();
system.out.println("total" + Text);

But this code is not working. How do I properly get to the value in the "spnLogCount" tag?

但是这段代码不起作用。如何正确获取“spnLogCount”标签中的值?

采纳答案by eduliant

public static void main(String[] args) {
        // TODO Auto-generated method stub

        WebDriver driver = new FirefoxDriver();
        driver.get("file:///C:/Users/rajnish/Desktop/my.html");

        // way one
        // you can create your custom x path
        // one x path can be made directly using id of the span like 
        // xpath =  //span[@id='spnLogCount']
        // also not if you are not sure of the tag name then you can also use * in xpath like below
        String myText = driver.findElement(By.xpath("//*[@id='dvCount']/span[2]")).getText();
        System.out.println("Total Log Count :  " + myText);

        // way two
        // you can directly use id 
        myText = driver.findElement(By.id("spnLogCount")).getText();
        System.out.println("Total Log Count :  " + myText);

        // way three
        // if you are using css selector then for id you can use #
        myText = driver.findElement(By.cssSelector("#spnLogCount")).getText();
        System.out.println("Total Log Count :  " + myText);

    }

UPDATE

更新

 driver.findElement(By.id("ui-id-3")).click();
             driver.findElement(By.linkText("Info Log")).click();

             driver.findElement(By.id("txtMessage")).sendKeys("Push Success");

             driver.findElement(By.id("txtMachineName")).sendKeys("AC204");

             driver.findElement(By.id("txtPortal")).sendKeys("91");

             driver.findElement(By.id("btnSearch")).click();

            // use it just before the sendkeys code like this 
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='dvCount']/span[2]")));
            String text = driver.findElement(By.xpath("//*[@id='dvCount']/span[2]")).getText();
            System.out.println(text);
Hope this helps 

回答by djangofan

I think you want something like this:

我想你想要这样的东西:

WebElement textElement = driver.findElement(By.xpath(".//div/span"));
String text = textElement.getText();
System.out.println("Value: " + text);

回答by Srikanth Nakka

String text=driver.findElement(By.xpath("//span[@id='spnLogCount']")).getText();
System.out.println(text);

回答by Sadik Ali

I updated you code, please use below it will work.

我更新了你的代码,请在下面使用它会起作用。

String Text= driver.findElement(By.cssselector("#spnLogCount")).gettext();
System.out.println("total" + Text);

回答by Boni García

As a JUnit test (and using WebDriverManagerto handle the gecko driver):

作为 JUnit 测试(并使用WebDriverManager处理壁虎驱动程序):

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.github.bonigarcia.wdm.FirefoxDriverManager;

public class FirefoxTest {

    private WebDriver driver;

    @BeforeClass
    public static void setupClass() {
        FirefoxDriverManager.getInstance().setup();
    }

    @Before
    public void setup() {
        driver = new FirefoxDriver();
    }

    @After
    public void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    public void test() {
        driver.get("http://localhost:8080"); // Here your URL
        WebElement webElement = driver.findElement(By.id("spnLogCount"));
        System.out.println(webElement.getText());
    }

}