Search code examples
seleniumselenium-webdriverwebdriver

Selenium webdriver element identification fails on machine-generated webpage


I am facing a problem identifying elements on an webpage generated by a machine. I am using Selenium Webdriver for Java and Google Chrome as browser.

So, there are ids and values that look very unusually. I tried to identify the elements by id and xpath so far. Here's some code to my problem.

The select box i want to identify:

<select id="singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person" class="form-control" aria-expanded="false" aria-labelledby="lbl_192_982637" aria-describedby="singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person_Sth"><option aria-selected="false" disabled="" style="display:none" aria-hidden="true"></option><option id="singleselect-option-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person[0]" value="OK" aria-selected="false">Ja, die Gehaltsnachweise liegen vor</option><option id="singleselect-option-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person[1]" value="NOK" aria-selected="false">Ja, es bestehen aber Formmängel</option><option id="singleselect-option-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person[2]" value="NO" aria-selected="false">Nein, die Gehaltsnachweise liegen nicht vor</option><option id="singleselect-option-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person[3]" value="WAIVER" aria-selected="false">Unterlagsverzicht</option></select>

Full XPath is:

/html/body/div[1]/div[2]/div[2]/div[1]/div[2]/div/div[2]/div/div/div/div[2]/div[1]/div[2]/div/div[2]/div/div[2]/div/div[1]/div/div/select

The relative xPath is:

//*[@id="singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person"]

Idetification by id ("singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:VPP001_Person") fails also like by xpath, even that the xpath entered in Google Chrome is valid:

enter image description here

Unfortunately i cannot post the whole html page but maybe someone has a clue here. Its a big page, maybe the scrolling is the problem as not all items are visible art once in window?

Last try from first comment (checked the preconditions 1/1)

enter image description here

Translated into Java:

WebDriverWait wait = new WebDriverWait(driver, 30);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select[id^='singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:'][aria-describedby^='singleselect-Vertical_Layout']")));
        WebElement select = driver.findElement(By.cssSelector("select[id^='singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:'][aria-describedby^='singleselect-Vertical_Layout']"));

...fails with:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.cssSelector: select[id^='singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:'][aria-describedby^='singleselect-Vertical_Layout'] (tried for 30 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at com.commerzbank.test.tool.service.e2e.ChromeTest.main(ChromeTest.java:74)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"select[id^='singleselect-Vertical_Layout2:QuestionGroup1[0]:QuestionGroup:Vertical_Layout1:Question1[0]:Question:Horizontal_Layout1:'][aria-describedby^='singleselect-Vertical_Layout']"}
  (Session info: chrome=97.0.4692.71)

Solution

  • Long research, short answer. The issue have been some very deep nested iframes, two in my specitfic case, on my big web page. I solved it with the following code snippet:

     wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//iframe[contains(@title, 'Manual')]")));
            WebElement parentFrame = driver.findElement(By.xpath("//iframe[contains(@title, 'Manual')]"));
            driver.switchTo().frame(parentFrame);
            wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//iframe[contains(@title, 'Test')]"))));
            WebElement childFrame = driver.findElement(By.xpath("//iframe[contains(@title, 'Test')]"));
            driver.switchTo().frame(childFrame);
    

    After switching to the child iframe i could find my element. Cheers.