1
votes

I am trying to select this element using chromedriver selenium:

<span title="[email protected]" class="rcmContactAddress">[email protected]</span>

where the email address is a variable.

Tried this, but doesn't work:

driver.FindElement(By.XPath("//span[text()='" + username + "@ama-coupons.com']")).Click();

also tried this:

driver.FindElement(By.XPath("//span[contains(@title, '"+username+"@ama-coupons.com']")).Click();

but it doesn't work either. #1 gives element not found #2 gives not a valid xpath selector.

UPDATE:

I had it work with a hard-coded email address, however, as soon as I replace that email address with a variable, it doesn't work anymore.

HTML Element:

<span title="[email protected]" class="rcmContactAddress">[email protected]</span>

code:

new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("span[title='"+email+"']"))).Click(); Thread.Sleep(1000);

Error: (which also shows the value of email:)

Inner Exception 1:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"span[title='[email protected]']"}
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

1

1 Answers

1
votes

There is problem with your second selector you forgot ) inside selector:

By.XPath("//span[contains(@title, '[email protected]')]")

For element not found error use wait, here code with explicit wait until element is clickable:

new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("span[title='[email protected]']"))).Click();

For more information about waits you can find in Selenium Docs or When to use explicit wait vs implicit wait in Selenium Webdriver?

If element is inside iframe Selenium: Unable to access iframe and data inside it

Selectors be used as well:

By.CssSelector("span[title='[email protected]']")
By.XPath("//span[.='[email protected]']")