1

Following code uses non-headless chrome and it works:

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait


chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--start-maximized')
chrome_options.binary_location = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'

driver = webdriver.Chrome(executable_path=os.path.abspath("C:\User\Program Files\chrome-driver\chromedriver.exe"))

driver.set_window_size(1200, 600) 

driver.get("login-url")
driver.find_element_by_id("loginId").send_keys("uname")
driver.find_element_by_id("newPassword").send_keys("pwd")
driver.find_element_by_name("submit-button").click()
driver.set_window_size(1200, 800)
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))

v = driver.find_element_by_xpath("//tr[4]/td[5]/span").text

print(v)

When I choose to use headless chrome:

driver = webdriver.Chrome(executable_path=os.path.abspath("C:\User\Program Files\chrome-driver\chromedriver.exe"), chrome_options=chrome_options)

It throws following exception:

Traceback (most recent call last):
  File "C:/User/workspaces/pyworkspaces/fin2/venv/process.py", line 28, in <module>
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))
  File "C:\User\workspaces\pyworkspaces\fin2\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

So it seems to fail on following line, in headless chrome:

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))

I also tried presence_of_element_located:

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"user-info")))

But it still gives TimeOutException. Why is it so?

MsA
  • 2,599
  • 3
  • 22
  • 47

2 Answers2

0

What I did in the past is adding the argument '--start-maximized':

chrome_options.add_argument('--start-maximized')

Try it hope it helps you!

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
0

Instead of using presence_of_element_located() you need to wait for visibility_of_element_located() and you can use the following Locator Strategy:

  • Using ID:

    element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))
    
  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#user-info")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='user-info']")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I didnt get "As you intent to send a character sequence within the element". I just want to wait till a webpage with element having `id=user-info` loads. Tried replacing `WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"user-info")))` with `WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='user-info']")))`. Giving same exception. – MsA Jun 20 '19 at 09:19
  • @anir You haven't mentioned about your specific usecase on what you intend to do after _...waiting for webpage with that element is obtained..._. Initially I assumed it to be _UserID_ field but now it seems it's _User Info_ field. So changed the code. Let me know the status. – undetected Selenium Jun 20 '19 at 09:27
  • mistakenly I re-asked same question. Now copied content of that question in this one because that was well written with stack trace. Does your above answer resolves it? – MsA Jun 25 '19 at 13:46