I am using Java and Selenium to write a test, I use the code below to get into Chrome:setting
driverChrome.manage().window().maximize(); driverChrome.get("chrome://settings");
But when the page is open I can not find any of its web Elements, for example when I try to find “show advanced setting….” by this code
driverChrome.findElement(By.xpath("//a[@id='advanced-settings-expander']")).click();
it throws an error saying that “no such element: Unable to locate element” I tried to located other elements, but they all failed. I saw this post here but it did not help.
Find the code below:
driverChrome.manage().window().maximize(); driverChrome.get("chrome://settings"); Thread.sleep(5000); WebElement w = driverChrome.findElement(By .xpath("//iframe[@name='settings']")); driverChrome = driverChrome.switchTo().frame(w); Thread.sleep(1000); while (true) { try { WebElement we = w.findElement(By .xpath("//a[text()='Show advanced settings...']")); if (we.isDisplayed()) { we.click(); Thread.sleep(1000); break; } } catch (Exception e) { System.out.println(e.getMessage()); System.out.println("========================="); } }
Advertisement
Answer
I haven’t tested this but I took your code snippet and cleaned it up a bit. Try this and see if it works. This should be pretty close.
Once you switch to the IFRAME context, you don’t need to reference the IFRAME
as you did with w.findElement()
.
In general, Thread.sleep()
is not a good practice. You should prefer to use WebDriverWait
with ExpectedConditions
. Check the docs for all the different things you can wait for using ExpectedConditions
. I used .elementToBeClickable()
in my code below. This is perfect since you want to click an element. The .until()
returns the element waited for so you can just append .click()
on the end of the statement… or you can store the element in a WebElement
variable and use it elsewhere.
driverChrome.manage().window().maximize(); driverChrome.get("chrome://settings"); WebElement w = driverChrome.findElement(By.xpath("//iframe[@name='settings']")); driverChrome = driverChrome.switchTo().frame(w); WebDriverWait wait = new WebDriverWait(driverChrome, 10); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Show advanced settings...']"))).click(); // alternative example... store returned element and then click on a separate line... or use the variable elsewhere, etc. // WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Show advanced settings...']"))); // link.click();