0

I'm using selenium 3.4.0 and trying to click an element on a page. I just used Thread.sleep(12000) (which is max wait) before I perform click action but still it throws nosuchelement exception. I used other browsers like chomre --headless but it works pretty perfect and similarly it works fine with firefox and chorme UI browsers as well.

Code:

    WebDriver iDriver=new HtmlUnitDriver();     
    iDriver.get("https://www.xyz:8080/#!/test/createTest");
    String title = iDriver.getTitle();
    System.out.println(title);  
    iDriver.manage().window().setSize(new Dimension(1920, 1200));   
    Thread.sleep(13000);
    org.testng.Assert.assertEquals("UI", title);    
    JavascriptExecutor executor = (JavascriptExecutor)iDriver;
    executor.executeScript("arguments[0].click();", iDriver.findElement(By.xpath(".//span[@class='path']//a[contains(@href,'/testresource/createTestRequest')]")));

Tried with Javascriptexecutor.click() as above as well normal click() method but both didn't work.

The same above script executing perfectly fine with other browsers.

Exception:

   org.openqa.selenium.NoSuchElementException: Unable to locate a node using //span[@class='path']//a[contains(@href,'/testresource/createTestRequest')]
   For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
   Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
   System info: host: 'L-MAA-25006133', ip: '10.229.101.11', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'
   Driver info: driver.version: HtmlUnitDriver

Ideally I just want to execute the above script in linux box which is in Google Cloud Platform. And I dont have permission to install any browser like (chrome, firefox binaries) over there to execute the scripts against such browsers. Tried chrome --headless option but It still looking Chrome binary (/usr/bin/chrome-browser) in physical path to set in chromeoptions. We can't do that on that machine. So, I just written script where I no need to specify any browser binary and it will execute as headless browser without support of chrome or firefox binaries.

Please give me you thoughts about this. If there is any better way to do this I will try to do in that way as well.

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84
  • One of the biggest issues I had with a `NoSuchElementException` was it not being able to find the element, the easiest way for me to fix that was to add an ID or Name to the element and the just search for it based on the ID or Name, have you tried that? – Jonathan Arendt Feb 20 '19 at 01:17
  • Also if the page isn't loaded when you try to call and the `thread.sleep()` is already at its max value, you could try adding another call to `thread.sleep()` to give the browser more time to load. – Jonathan Arendt Feb 20 '19 at 01:19
  • I increased the thread.sleep till 40sec but still no luck. I have to run multiple data against this script if it takes more than 40sec for each iteration. It looks very bad in performance. Its also complex web page I cant easily go ahead with name or id for that. – ArrchanaMohan Feb 20 '19 at 02:26
  • If you can't add a name or I'd, you might want to check your xpath. – Jonathan Arendt Feb 20 '19 at 02:43
  • Just tried to hit the id alone (just for ensure it identifying the id element) but still no luck. used thread.sleep(40000); – ArrchanaMohan Feb 20 '19 at 02:47
  • The xpath is valid one and it works fine all other browsers. I just get the getPageSource() of iDriver. And observed only parent div tag being captured like below:
    if you expand this ui-container only we can see the above elements but the pagesource get only parent instead of all it's siblings.
    – ArrchanaMohan Feb 20 '19 at 02:48
  • Which browser are you forced to use? – Jonathan Arendt Feb 20 '19 at 03:19
  • I didn't use any browser. I don't have to... – ArrchanaMohan Feb 20 '19 at 04:51
  • Can you check that the element you are trying to click is in iframe ? – Sameer Arora Feb 20 '19 at 05:36
  • Nope. Im pretty sure there is no iframe in dom – ArrchanaMohan Feb 20 '19 at 05:47
  • This might be needed, let me know if it works. `iDriver.setJavascriptEnabled(true);` – Jonathan Arendt Feb 20 '19 at 08:26
  • I read that Javascript is disabled by default, but just you know, it may actually differently from other browsers, but I do hope it helps. – Jonathan Arendt Feb 20 '19 at 08:27

0 Answers0