I'm using Selenium Webdriver with ChromeDriver. Mastering my skills with XPath.
Here is the piece of code I tried to execute:
WebDriver driver;
String baseURL = "http://youtube.com";
System.setProperty("webdriver.chrome.driver", "/Users/svetlana_yatsushko/Documents/workspace/selenium/chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(baseURL);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id='dismiss-button']//descendant::yt-formatted-string[1]")).click();
I've got an error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='dismiss-button']//descendant::yt-formatted-string[1]"}
Also I tried to write other XPaths options like
//*[@aria-label='No, thanks']//*[@id='button']//*[text()='No, thanks']
But no one works stable, though these XPaths have been found in Elements and Console and have 1 matching node.
I noticed a situation, it can be found 2 times out of 5, for example. Or sometimes it is not found 5 times in a row... Probably this is on an XPath issue, but I'm not able to get the reason.
Hopefully you had something similar and could help me.