1

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("=========================");
        }
    }
Community
  • 1
  • 1
LoveLovelyJava
  • 735
  • 4
  • 9
  • 21
  • What settings are you trying to modify? Instead of opening the settings page. Use [`ChromeOptions`](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/chrome/ChromeOptions.html) to modify any settings. – JRodDynamite Oct 19 '15 at 17:34
  • @JRodDynamite you already helped me with that [here](http://stackoverflow.com/questions/33218794/enabling-popup-windows-by-selenium) :) but before your help I was trying to do it through Chrome:settings and figured out that I can not locate any web elements, so just out of curiosity I want to know why I can not get ay elements from there – LoveLovelyJava Oct 19 '15 at 17:41
  • 1
    switch to iframe, then try to click. – Turcia Oct 19 '15 at 18:15
  • That particular element is inside `` – JeffC Oct 19 '15 at 18:16
  • @JeffC I used this code: **WebElement we = driverChrome.findElement(By.xpath("//iframe[@name='settings']//a[text()='Show advanced settings...']"));** to get into the iframe and fetch the element bur it failed again. please help. – LoveLovelyJava Oct 19 '15 at 18:38
  • You will need to switch to the frame first. A quick google search will show you examples of how to do this. – JeffC Oct 19 '15 at 20:26
  • @JeffC I added a peace of code to my qoestion, please have a look to it, there I added **riverChrome = driverChrome.switchTo().frame(w);** at the first, please let me know if there is any problem with the code above, thanks – LoveLovelyJava Oct 19 '15 at 20:40

2 Answers2

2

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();
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Thanks indeed, just a question, why does **w.findElement(bla bla)** throw errors? – LoveLovelyJava Oct 20 '15 at 15:22
  • It's not something I've explored but if I were to guess, I would say that the way Selenium implements access to `IFRAME`s is by context... you use `.switchTo()` to navigate into that `IFRAME`'s context so `w.findElement()` doesn't make sense. When you are in the default context, you can't see elements inside `w`. When you are inside `w`'s context, you don't see `w`. – JeffC Oct 20 '15 at 15:27
0

You will need to identify Shadow roots to interact with a lot of Chromes native pages including the Settings page.
See this Thread on how to work with them properly:
How to interact with the elements within #shadow-root (open) while Clearing Browsing Data of Chrome Browser using cssSelector