Select drop down option using Selenium. Headless chrome. Python

余生颓废 提交于 2020-01-24 16:39:39

问题


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:

回答1:


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)


来源:https://stackoverflow.com/questions/58770745/select-drop-down-option-using-selenium-headless-chrome-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!