1

I am trying to select this element using chromedriver selenium:

<span title="1968milziewa@ama-coupons.com" class="rcmContactAddress">1968milziewa@ama-coupons.com</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="1959jessamin@ama-coupons.com" class="rcmContactAddress">1959jessamin@ama-coupons.com</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='1959Jessamin@ama-coupons.com']"}
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Martijn Deleij
  • 91
  • 1
  • 16

1 Answers1

2

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

By.XPath("//span[contains(@title, '1968milziewa@ama-coupons.com')]")

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='1968milziewa@ama-coupons.com']"))).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='1968milziewa@ama-coupons.com']")
By.XPath("//span[.='1968milziewa@ama-coupons.com']")
Sers
  • 12,047
  • 2
  • 12
  • 31