Internet Explorer not selecting Elements on Webpage

110 Views Asked by At

I am running Selenium to test a specific area of my companies webpage. What I am trying to do seems relatively easy in theory, but I have ran into several obstacles. Can someone please tell me why the URL is opening but won't select the "Services" Hyperlink?

Below is a snippet of the code:

System.setProperty("webdriver.ie.driver","Path to IE/IEDriverServer_64.exe");
WebDriver driver = new InternetExplorerDriver();
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
driver.get("https://www.ups.com/us/en/Home.page");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*/div[@id='ups-header']/nav[@id='ups-navItems']/ul[@class='ups-navItems_primary']/li[@class='ups-navMenu ups-menu'][3]/a[@id='ups-menuLinks2']")).click();

As I stated earlier, in theory this should open up UPS's homepage and select the "Services" tab on the top right of the page. Instead it just goes to UPS.com homepage and stays there.

I have driver.findElement(By.xpath........); in this example but I have tried findElement(By.name & partialLinkText

Can anyone give me a solution besides update to latest version(s)?

Metadata: Windows 10, JAVA 10, Internet Explorer (Unfortunately) 11.4.

Thanks in advance!

Edit with additonal HTML structure:

This is a portion of the HTML I am working with. This HTML belongs to the Services link I want to click in my automation:

<a role="button" href="#" class="ups-analytics ups-menu_toggle" data- 
content-block-id="M1" data-event-id="22" aria-expanded="false" id="ups- 
menuLinks2" aria-controls="ups-menuPanel2">Services<span class="ups-mobnav- 
arrow" aria-hidden="true"></span></a><div class="ups-menu_list ups-cols-3" 
aria-hidden="true" role="region" id="ups-menuPanel2" aria-labelledby="ups- 
menuLinks2">
<h2 class="ups-med_show">Services</h2>
<div class="ups-menu_listCols">     
2

There are 2 best solutions below

1
On

Internet Explorer v11 doesn't opens the url on my system but with Selenium v3.12.0, ChromeDriver 2.39 and Chrome v67.0 the following solution clicks on the element with text as Services just perfecto:

  • Code Block:

    System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions"); 
    WebDriver driver =  new ChromeDriver(options);
    driver.get("https://www.ups.com/us/en/Home.page");
    driver.findElement(By.linkText("Services")).click();
    
  • Browser Snapshot:

Services_clicked

0
On

This was tricky for IE , for some reasons all the xpath i tried seems to not work. This might be due to some capabitlites that needs to be added. I haven't looked into it in details but its working for me in Firefox:

        String baseURL = "https://www.ups.com/us/en/Home.page";
    WebDriver driver;

    System.setProperty("webdriver.gecko.driver", "//path to//geckodriver.exe");
    driver = new FirefoxDriver();
    driver.manage().window().maximize();

    driver.get(baseURL);
    Thread.sleep(3000);
    WebElement service=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/div/div/div/header/div/nav/ul[1]/li[3]"));
    System.out.println(service.getText());
    service.click();

If you have issues with your Firefox. Please share the stacktrace so we can help. This code perfectly works for Firefox