使用selenium webdriver打印所需文本

时间:2016-04-11 17:58:51

标签: java selenium selenium-webdriver

下面是HTML查询。

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

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

我使用以下代码,但我无法获得值:

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

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

5 个答案:

答案 0 :(得分:1)

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

    }

更新

 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 

答案 1 :(得分:0)

我想你想要这样的东西:

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

答案 2 :(得分:0)

ListView

答案 3 :(得分:0)

我更新了您的代码,请在下方使用它。

class DisableMigrations(object):

    def __contains__(self, item):
        return True

    def __getitem__(self, item):
        return "notmigrations"


TESTS_IN_PROGRESS = False
if 'test' in sys.argv[1:]:
    logging.disable(logging.CRITICAL)
    PASSWORD_HASHERS = (
        'django.contrib.auth.hashers.MD5PasswordHasher',
    )
    DEBUG = False
    TEMPLATE_DEBUG = False
    TESTS_IN_PROGRESS = True
    MIGRATION_MODULES = DisableMigrations()

答案 4 :(得分:0)

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

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());
    }

}