简体   繁体   中英

How get value from field using Selenium java

I try to get value (email adress) from page https://temp-mail.org/ , and put it to the string.

So my code is this:

maven dependencies:

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>

    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>4.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.8</version>
        <scope>test</scope>
    </dependency>
</dependencies>

I create Abstract page

public abstract class AbstractPage {
    protected WebDriver driver;

    protected AbstractPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
    protected abstract AbstractPage openPage(String url);
}

TempMailPage:

public class TempMailOrgPage extends AbstractPage {
    String regex = "^[A-Za-z0-9+_.-]+@(.+)$";
    Pattern pattern = Pattern.compile(regex);
    
    public TempMailOrgPage(WebDriver driver) {
        super(driver);
    }
    
    public TempMailOrgPage openPage(String url) {
        driver.get(url);
        driver.manage().window().maximize();
        return this;
    }
    @FindBy(xpath = "//input[@id='mail']")
    WebElement mailField;

    public TempMailOrgPage getAddress() {
        //new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElement(mailField, regex));
        new WebDriverWait(driver, 10).until(ExpectedConditions.textMatches((By) mailField, pattern));
        System.out.println(mailField.getAttribute("value"));
        return this;
    }
}

and Test

public class MainTest {
    WebDriver driver;
    
    @BeforeMethod(alwaysRun = true)
    public void browserRun() {
        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();
    }
    @Test(description = "try get addresb")
    public void scenarioWithCopyingEmail()  {
        new TempMailOrgPage(this.driver)
                .openPage("https://temp-mail.org/")
                .getAddress();
    }
    
    @AfterMethod(alwaysRun = true)
    public void afterTestCompleted() {
        driver.quit();
        driver = null;
    }
}

And after test running I get error. I understand that a mistake in waiting then email address is generating.

And instead of a word "Loading" will be mail address. In this line I try to use pattern:

 new WebDriverWait(driver, 10).until(ExpectedConditions.textMatches((By) mailField, pattern));

What am I doing wrong with waiting?

Ps using thread.sleep not a way.

Problem

I think the problem is that web element [mailField] don't actually have a text variable.

I try the following code:

driver.get("https://temp-mail.org");
Thread.sleep(60000);
System.out.println(driver.findElementById("mail").getText());

waiting for 60 second and although the ui display the email. The system still print out blank element. The method [textMatches] will get the text of the web element to compare with the regrex. When the web element don't have text or text is blank, how it suppose to compare. So of course it will always throw out time out exepception.

Solution

I can not find other method which work actually like [textMatches] and able to apply to [getAttribue("value")] . So you can concern this solution as a work arround. Waiting for attribute value contain [@] .

new WebDriverWait(driver, 20)
    .until(ExpectedConditions
    .attributeContains(driver.findElementById("mail"), "value", "@"));

System.out.println(driver.findElementById("mail").getAttribute("value"));

Working pretty well when i tested

borik68628@heroulo.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM