0

I'm having trouble clicking the following button on Outlook:

<a data-m="{"cN":"SIGNIN", "sN":"P6", "pV":"1"}" href="https://outlook.live.com/owa/?nlp=1" class="internal sign-in-link" data-task="signin">Sign in</a>

I've attempted both of the following code:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(text(), 'Sign in')]"))).click();

and

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@data-task='signin']"))).click();

The terminal sends a vague NoSuchElementException for both of them.

Could the Xpaths be incorrect?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

You can copy full Xpath from Google Chrome browser and just paste in your code. To copy it press Ctrl+Shift+I, find the element and click on Copy > Copy Full Xpath.

This is your full xpath:

/html/body/header/div/aside/div/nav/ul/li[2]/a

This python code below works for me:

#python solution
from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get("https://outlook.live.com/")
time.sleep(3)
driver.find_element_by_xpath('/html/body/header/div/aside/div/nav/ul/li[2]/a').click()
CODPUD
  • 11
  • 5
0

The element is a dynamic element so to click() on the element with text as Sign in instead of using ExpectedConditions of visibilityOfElementLocated() you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • linkText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Sign in"))).click();
    
  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.internal.sign-in-link[data-task='signin']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='internal sign-in-link' and text()='Sign in']"))).click();
    

References

You can find a couple of relevant detailed discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352