user1433077
user1433077

Reputation: 1

how to grab email from the place holder using selenium webdriver

How do I grab the value from the below code in selenium webdriver using java

<script id="metamorph-0-start" type="text/x-placeholder"/>
[email protected]
<script id="metamorph-0-end" type="text/x-placeholder"/>

Upvotes: 0

Views: 576

Answers (2)

lukeis
lukeis

Reputation: 1983

I might be tempted to answer:

String email = driver.findElement(By.xpath("id('metamorph-0-start')/..')")).getText();

But that will only work if there are no other text siblings/descendants.

I'm going to assume a few things, one is that your html actually is:

<script id="metamorph-0-start" type="text/x-placeholder"></script>
[email protected]
<script id="metamorph-0-end" type="text/x-placeholder"></script>

To really just get the email I would do this using Javascript:

String email = (String) ((JavascriptExecutor)driver).executeScript(
    "return arguments[0].nextSibling.textContent.split('\\n')[1]",
    driver.findElement(By.id("metamorph-0-start")));

You could also do the newline stripping in Java instead of in Javascript.

Upvotes: 1

Petr Janeček
Petr Janeček

Reputation: 38444

Without more knowledge about the page (or the containing element), I think TunaBum has the best answer.

That said, based on the assumption that that's the only e-mail address (or rather text with an at-sign in int) on the page, you could do

String email = driver.findElement(By.xpath("//*[contains(text(),'@')]")).getText();

that gets text of an element which contains an @ in its text.

If your containing element has multiple metamorph-number-start/end in it, and every one of them contain some text, then JS is your only path since WebDriver can't yet work with text nodes.

Upvotes: 0

Related Questions