Java Selenium - Can't seem to select an element

时间:2015-06-15 14:37:07

标签: java selenium xpath hidden

I'm trying to select an e-mail href to grab the text, but no matter what selections I use to try to select the e-mail, my selection doesn't seem to work and I am curious what other ways might exist to fix the problem.

I have used absolute and relative xpath, CSS selector, and id to try to grab the e-mail. I've tried using both visibilityOfElementLocated and elementToBeClickable to no avail. Here is the current iteration I have, just for reference (illustrating both my cssSelector and xpath selector):

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".email")));
String hire_email = driver.findElement(By.xpath(".//*[@id='information-row-0']/div/div/div[2]/div/div[1]/div")).getText();

Whenever I run the automation, when it gets to the e-mail step, it will wait 15 seconds before it times out which clearly indicates it is not seeing the element.

I use Firebug and Firepath for xpath, so I know my xpaths are not wrong when trying to select the element which is what confuses me the most. What might be causing this element to be unseen and what can I try to do to solve this problem? Please let me know if there is other relevant information needed that is being left out and I'll provide it if I can. Thanks in advance.

As per request, here is the tr and td information in the table where the email resides... (sorry it looks ugly, copying directly from Firefox)

<tr class="information-row opened">
<td class="information-row opened" colspan="8">
<div id="information-row-0">
<div class="information-row-shell" style="overflow: visible; height: 238px;">
<div class="minimal-row row-fluid">
<div class="ja-pulse-container">
<div class="mrg-10 pull-left ja-pulse" style="display: block;">
</div>
<div class="information-row-rest row-fluid">
<div class="row-fluid">
<div class="span4 contact-container row-fluid">
<div class="caption">
<address>
<div class="full-name">
<span class="full-address">
<br>
<span class="full-address"> lanse, MI 49946 </span>
<hr>
<i class="icon icon-mobile-phone"></i>
<abbr title="Phone">Primary:</abbr>
<span class="tel">(432) 777-4444</span>
<br>
<i class="icon icon-phone"></i>
<abbr title="Additional phone">Secondary:</abbr>
<span class="tel">N/A</span>
<br>
<i class="icon icon-shield"></i>
<abbr title="Social Security Number"> SSN: </abbr>
<span class="ssn">(removed, even though this is a fake testing profile)</span>
<br>
<br>

<a class="email" href="mailto:alaiaia@lssiss.com">
<i class="icon icon-envelope"></i>
<span class="email">alaiaia@lssiss.com</span> <!--- I want to grab this --->
</a>
</address>
</div>

2 个答案:

答案 0 :(得分:1)

Do you get any exceptions thrown? if the element is not visible then a NoSuchElementException will be thrown. Other then that, i would suggest making your xpath more readable, something like the following:

String emailAddress = driver.findElement(By.xpath("//span[@class='email']")).getText();

答案 1 :(得分:0)

Can you not locate it like this?

driver.findElement(By.xpath("//span[@class='email']"));
相关问题