Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webdriver.chrome.driver in headless mode can't find element #4477

Closed
plaidshirtakos opened this issue Aug 16, 2017 · 14 comments
Closed

webdriver.chrome.driver in headless mode can't find element #4477

plaidshirtakos opened this issue Aug 16, 2017 · 14 comments

Comments

@plaidshirtakos
Copy link

@plaidshirtakos plaidshirtakos commented Aug 16, 2017

Meta -

OS:
Windows 10
Selenium Version:
2.53.1
Browser:
ChromeDriver 2.31.488763

Expected Behavior -

I initialized driver as shown below:

ChromeOptions chromeOptions = new ChromeOptions();
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
chromeOptions.addArguments("--headless");
driver = new ChromeDriver(chromeOptions);
driver.manage().window().maximize();

Given xpath belongs to an input field, which opens a file selector when clicked. It is working perfectly in a normal Firefox. I use WebDriverWait to wait until necessary elements appear.

wait.until(ExpectedConditions.presenceOfElementLocated(mainPage.InputFileField));

Actual Behavior -

I get following message, when I execute test:

Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 44655 Only local connections are allowed.

org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for presence of element located by: By.xpath:

Steps to reproduce -

Please see code snippet.

@kumar-nitish

This comment has been minimized.

Copy link
Contributor

@kumar-nitish kumar-nitish commented Aug 16, 2017

Could you please share the xpath and the web page(or it's DOM) ?

@barancev

This comment has been minimized.

Copy link
Member

@barancev barancev commented Aug 16, 2017

Yes, please provide a concise reproducible test case

@jleyba

This comment has been minimized.

Copy link
Contributor

@jleyba jleyba commented Aug 16, 2017

driver.manage().window().maximize(); doesn't work in headless mode. Try setting the window size directly.

For more info, see https://bugs.chromium.org/p/chromedriver/issues/detail?id=1901

@jleyba jleyba closed this Aug 16, 2017
@plaidshirtakos

This comment has been minimized.

Copy link
Author

@plaidshirtakos plaidshirtakos commented Aug 17, 2017

@kumar-nitish : Xpath for this element look like this: //*[@id="form"]/p[1]/label
@jleyba : I tried to set it with --window-size=1024,768 too.

@kumar-nitish

This comment has been minimized.

Copy link
Contributor

@kumar-nitish kumar-nitish commented Aug 20, 2017

I created a dummy page and tested similar xpath with below config and it worked fine.

  • Win10 x64
  • Chrome driver v2.31.488763
  • Selenium 2.53.1
  • Chrome : v59 & 60 ( latest )
  • Xpath : //*[@id='form']/p[1]/input
  • HTML
<html>
<body>
    <form id="form">
        <p>
            <input type="file" name="inputFile1" />
        </p>
    </form>
</body>
</html>

@plaidshirtakos Could you please share your html DOM, chrome version and exact XPATH ?

@plaidshirtakos

This comment has been minimized.

Copy link
Author

@plaidshirtakos plaidshirtakos commented Aug 22, 2017

@kumar-nitish : //*[@id="formA"]/p[1]/label
Browser:
ChromeDriver 2.31.488763
<label class="forFileInput" for="fileUpA" multiple="">Choose file!</label>

@kumar-nitish

This comment has been minimized.

Copy link
Contributor

@kumar-nitish kumar-nitish commented Aug 22, 2017

Could you please follow below instructions and share the outcome

  1. Create a html file with below code
<html>
<body>
    <div>
    <form id="form">
        <p>
            <label class="forFileInput" for="fileUpA" multiple="">Choose File!</label>
            <input type="file" id="fileUpA" />
        </p>
    </form>
    </div>
</body>
</html>
  1. Download TestNg test case from https://gist.github.com/kumar-nitish/9d213cfcaac5c160b08a3816ebb7325f
  2. Update the chrome driver and html page path in above code and execute it
@plaidshirtakos

This comment has been minimized.

Copy link
Author

@plaidshirtakos plaidshirtakos commented Aug 24, 2017

@kumar-nitish : I use the exact same code to cretae driver and get the following:

java.lang.NullPointerException

System.setProperty("webdriver.chrome.driver", Thread.currentThread().getContextClassLoader().getResource("C:\\chromedriver.exe").getFile());

@kumar-nitish

This comment has been minimized.

Copy link
Contributor

@kumar-nitish kumar-nitish commented Aug 24, 2017

My bad, that was because I was referring to chrome from resource folder. Use this instead

System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
@codebryo

This comment has been minimized.

Copy link

@codebryo codebryo commented Sep 13, 2017

@kumar-nitish I can at least verify that I run into the same kind of issue.
Headless chrome is not correctly picking up have_selector or has_selector or any other approach through Selenium to verify an element exists. I found a workaround though.
Not sure why, because it works on some elements and on some not.

Anyway, I ended up doing something like this (Note I am doing it in ruby)

# defined the method
def selector_exists(selector)
    page.execute_script "return document.querySelector('#{selector}').length > 0"  
end

# assert in the spec
expect(selector_exists('#form')).to be true

That at least solves it for now as the running js can verify it's there.

@Akshaya2312

This comment has been minimized.

Copy link

@Akshaya2312 Akshaya2312 commented Jan 16, 2018

not able to find below element while running test automation in chrome headless using nightwatch framework.
element is a video stream.
div[class$='src-routes-room-content-streams-___streams__streams src-routes-room-content-streams-___streams__size1']

@vasipower

This comment has been minimized.

Copy link

@vasipower vasipower commented May 1, 2019

Add below lines of code in ur main class

ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
options.addArguments("--window-size=1920,1080");
options.addArguments("--disable-gpu");
options.addArguments("--disable-extensions");
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
options.addArguments("--start-maximized");
options.addArguments("--headless");

WebDriver driver = new ChromeDriver(options);
@cgoldberg

This comment has been minimized.

Copy link
Contributor

@cgoldberg cgoldberg commented May 1, 2019

@vasipower This issue has been closed for over a year

Also.. don't just arbitrarily recommend Chrome options without explanation... it's harmful to any users that might mistake this as a solution.

@mushroomlb

This comment has been minimized.

Copy link

@mushroomlb mushroomlb commented May 10, 2019

I met the same situation. After studying, the following is correct:

self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.add_argument("--window-size=1920,1080")
self.chrome_options.add_argument("--disable-extensions")
self.chrome_options.add_argument("--proxy-server='direct://'")
self.chrome_options.add_argument("--proxy-bypass-list=*")
self.chrome_options.add_argument("--start-maximized")
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
self.chrome_options.add_argument('--disable-dev-shm-usage')
self.chrome_options.add_argument('--no-sandbox')
self.chrome_options.add_argument('--ignore-certificate-errors')
self.browser = webdriver.Chrome(options=self.chrome_options)

@lock lock bot locked and limited conversation to collaborators Aug 14, 2019
sunnybharel referenced this issue in sunnybharel/british_passport_checker Nov 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
9 participants
You can’t perform that action at this time.