AGlasencnik
AGlasencnik

Reputation: 159

Selenium doesn't find element

My Selenium application throws an error when I try to test it, even when I think the XPath is correct and using an explicit wait, it just throws the 'Element not found error'. Code:

string inputTextBoxXPath = "/html/body/div[@class='pages']/div[@class='main']/div[@class='game']/iframe/html/body/div[@class='main']/div[@class='bottom']/div[@class='round']/div[@class='selfTurn']/form/input[@class='styled']";
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement testIngame = wait.Until(e => e.FindElement(By.XPath(inputTextBoxXPath)));

Upvotes: 0

Views: 723

Answers (1)

Simon
Simon

Reputation: 143

I belive it's because of the iframe. You can't "xpath" into the iframe.

This happens because Selenium is only aware of the elements in the top level document. [...] Switching using a WebElement is the most flexible option.

//Store the web element
WebElement iframe = driver.findElement(By.cssSelector("#modal>iframe"));

//Switch to the frame
driver.switchTo().frame(iframe);

//Now we can click the button
driver.findElement(By.tagName("button")).click();

Check this out: https://www.selenium.dev/documentation/webdriver/browser/frames

Upvotes: 1

Related Questions