3

I am attempting to select an option from a drop down menu that expands the records on a page. It works fine when I am not running headless. When I run in headless I get a timeout exception error while the page waits to find the element.

<select name="ctl00$ContentPlaceHolder1$uxTabContracts$uxTabPanelWaintingApproval$uxGridList$ctl05$uxUCGridViewPagingTemplate$uxDropDownListPageSize" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$uxTabContracts$uxTabPanelWaintingApproval$uxGridList$ctl05$uxUCGridViewPagingTemplate$uxDropDownListPageSize\',\'\')', 0)" id="ContentPlaceHolder1_uxTabContracts_uxTabPanelWaintingApproval_uxGridList_uxUCGridViewPagingTemplate_uxDropDownListPageSize">
                <option value="5">5</option>
                <option value="10">10</option>
                <option value="20">20</option>
                <option value="30">30</option>
                <option value="40">40</option>
                <option value="50">50</option>
                <option value="100">100</option>
                <option value="250">250</option>
                <option value="500">500</option>
                <option selected="selected" value="1000">1000</option>

            </select>

I have tried using the XPATH, the ID, and Name.

wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="ContentPlaceHolder1_uxTabContracts_uxTabPanelWaintingApproval_uxGridList_uxUCGridViewPagingTemplate_uxDropDownListPageSize"]'))) # wait for option to expand page
    ExpandRecords = Select(chrome.find_element_by_xpath('//*[@id="ContentPlaceHolder1_uxTabContracts_uxTabPanelWaintingApproval_uxGridList_uxUCGridViewPagingTemplate_uxDropDownListPageSize"]')) # define element to expand page
    ExpandRecords.select_by_value('1000') # select page size option from dropdown

The expected result is to select '1000' records and that the page will expand and move on to the next piece of code which will select the necessary records I need. What happens is nothing. and I get a timeout exception.

line 289, in Import_To_CRM
    wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="ContentPlaceHolder1_uxTabContracts_uxTabPanelWaintingApproval_uxGridList_uxUCGridViewPagingTemplate_uxDropDownListPageSize"]'))) # wait for option to expand page
selenium.common.exceptions.TimeoutException: Message:
BigZA86
  • 113
  • 1
  • 6
  • cant you just run some javascript to directly set the value ? so `document.getElementById("selectionid").value = 1000` – William Bright Nov 08 '19 at 16:55
  • What headless browser driver are you using? PhantomJS? – John Gordon Nov 08 '19 at 16:57
  • @JohnGordon - I am using Chromedriver version 78. running Selenium, written in python. – BigZA86 Nov 08 '19 at 17:00
  • @WilliamBright - not sure how but I will see if i can find a way to try it. – BigZA86 Nov 08 '19 at 17:01
  • If you're using Chrome, how is that headless? I feel like I'm missing something. – John Gordon Nov 08 '19 at 17:02
  • @JohnGordon - There are options to run headless version of chrome. {options = webdriver.ChromeOptions() options.headless=True prefs = { "download.default_directory": r"M:\filepath", "download.prompt_for_download": False, "download.directory_upgrade": True} options.add_experimental_option('prefs', prefs) chromedriver = (r"C:\filepath\chromedriver.exe") chrome = webdriver.Chrome(executable_path=chromedriver, options=options)} – BigZA86 Nov 08 '19 at 17:05
  • Are you sure that `select` is visible and actual dropdown implemented in different elements like `div`? Also check if `select` is inside an `iframe`? Can you share the url? – Sers Nov 08 '19 at 17:16
  • @sers - Cant share the URL, It's an internal CRM. My issue is that it finds the element when I am turn off the headless option and run with an open window. So the issue is I can't actually see it when it is running headless to see the behavior differences. – BigZA86 Nov 08 '19 at 17:27
  • Try @Kunduk solution with chrome_options.add_argument('window-size=1920x1080'); and driver.execute_script("arguments[0].scrollIntoView();", element) – Sers Nov 08 '19 at 19:11

1 Answers1

3

For headless browser you have to set the window size to fire on event.Because headless browser can't recognize where to click without window size.

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('window-size=1920x1080');

If above option doesn't work then check if the website is blocking in headless mode using

print(driver.page_source)
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • 1
    This solved my problem. Thank you for the input, I didnt realize that running it headless would alter window size. – BigZA86 Nov 08 '19 at 19:45