Search code examples
seleniumxpathselenium-webdriverselenium-iedriver

Selenium XPath not found for IEDriver when I add URL/Domain in compatibility view


When I execute my script using IEDriver without compatibility view, my test script is running without any problem.

But, If I execute the same script after adding domain in compatibility view, then some elements are not found and I'm getting exceptions.

e.g. I want to get text of selected item from this DOM:

<select id="selectNumber" name="selectNumber" style="width:180px;">
    <option selected="selected" value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

and I'm using XPath .//*[@id='selectNumber']/option[@selected='selected'] to get text but it does not work.

I just checked that in IE DOM selected="selected" is not displayed for selected option until I change Document version manually.


Solution

  • You can use the Select class that works with every browser. Here's some code

    Select sel = new Select(driver.findElement(By.id("selectNumber")));
    WebElement selectOption = sel.getFirstSelectedOption();
    String text = selectOption.getText();