Python-Selenium:Chrome 无头设置不适用于“WebDriverWait”

Posted

技术标签:

【中文标题】Python-Selenium:Chrome 无头设置不适用于“WebDriverWait”【英文标题】:Python-Selenium: Chrome headless setting does not work with "WebDriverWait" 【发布时间】:2019-01-24 05:32:12 【问题描述】:

以下代码使用真实浏览器通过,但没有使用无头 chrome。

 while True:
    try:
        #Explicitly wait
        print("Try4")
        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "00BC0000008hfCI_listSelect")))
        print("Try5")
        ##Locate the select box
        s = driver.find_element_by_id("00BC0000008hfCI_listSelect")
        ##Unassigned queue
        Select(s).select_by_value("00BC0000008hqG8")
        time.sleep(1)
    except TimeoutException :
        print("Wrong!")
        break

结果将是:

Try4
Wrong!

headless chrome 好像找不到元素了。它适用于 Chrome 浏览器。我的 Selenium 版本是 3.14.0。

html 将如下所示:

<div class="topNav primaryPalette" id="00BC0000008hfCI_topNav"><div class="controls"><img src="/img/s.gif"  class="pageTitleIcon" title="Case">
<select class="title" id="00BC0000008hfCI_listSelect" name="fcf" onchange="ListViewport.instances['00BC0000008hfCI'].showFeedOrListView(this.options[this.selectedIndex].value)" title="View:"><option value="00BC0000009AFei">All Open Casa Cases</option>
    <option value="00B800000063tb2">All Open Cases</option>
    <option value="00B1A000009ZCok">Channel Upgrade Cases CCLTD</option>
    <option value="00B1A000009VKSg">Channel Upgrade Queue</option>
    <option value="00BC00000099n8S">China team's cases</option>
    <option value="00BC00000097nXg">Delete Queue</option>
    <option value="00BC00000097FPw">Handoff Case Queue</option>
    <option value="00B0y000009vcEI">Maintenance Windows Due Today</option>
    <option value="00B1A000009LY1L">mark r upgrade</option>
    <option value="00B1A000009V2x7">My close case</option>
    <option value="00BC000000976jF">My Open Cases</option>
    <option value="00B0y000009g0J9">My Open Cases - Final Solution Provided</option>
    <option value="00BC0000008hfCI" selected="selected">New Cases Created Today</option>
    <option value="00B0y000009vWOH">New Cases Created Today All</option>
    <option value="00B800000063tb0">Recently Viewed Cases</option>
    <option value="00BC00000099gHm">Stale Case View</option>
    <option value="00BC0000008hqG8">Unassigned Case Queue</option>
    </select><div class="filterLinks" id="00BC0000008hfCI_filterLinks"><a href="/ui/list/FilterEditPage?id=00BC0000008hfCI&amp;retURL=%2F500%3Ffcf%3D00BC0000008hfCI%26rolodexIndex%3D-1%26page%3D1">Clone</a> | <a id="create-new-view" href="/ui/list/FilterEditPage?ftype=t&amp;retURL=%2F500%3Ffcf%3D00BC0000008hfCI%26rolodexIndex%3D-1%26page%3D1">Create New View</a></div><span class="divisionLabel" id="00BC0000008hfCI_divisionLabel"></span><div class="topNavTab" id="00BC0000008hfCI_topNavTab"><div class="toggleButton" id="00BC0000008hfCI_toggleButton"><span class="listToggle chatterListToggle selected"><a href="javascript:void(0);" class="toggleLink" onclick="ListViewport.instances['00BC0000008hfCI'].toggleFeedListView(false)" title="View List - Selected"><span class="toggleIcon listIcon"></span><span class="linkText">List</span></a></span><span class="chatterListToggle chatterToggle clickable" id="ext-gen3"><a href="javascript:void(0);" class="toggleLink" onclick="ListViewport.instances['00BC0000008hfCI'].toggleFeedListView(true)" title="View Feed"><span class="toggleIcon chatterIcon"></span><span class="linkText">Feed</span></a></span></div><img src="/img/s.gif"  class="printerIconDisabled" id="00BC0000008hfCI_printLinkDisabled" style="display: none" title="Printable view is not available for feeds."><a href="javascript:printWin('/500/x?fcf=00BC0000008hfCI&amp;rolodexIndex=-1&amp;page=1')" id="00BC0000008hfCI_printLink" style="" title="Printable View (New Window)"><img src="/img/s.gif"  class="printerIcon" onblur="this.className = 'printerIcon';" onfocus="this.className = 'printerIconOn';" onmouseout="this.className = 'printerIcon';this.className = 'printerIcon';" onmouseover="this.className = 'printerIconOn';this.className = 'printerIconOn';" title="Printable View (New Window)"></a><a href="javascript:openPopupFocusEscapePounds(%27https://login.salesforce.com/services/auth/sso/00D30000000XsfGEAS/HTAuthProvider?startURL=%252Fapex%252Fhtdoor%253Floc%253Dhelp%2526target%253Dcases_view.htm%2526section%253DCases%2526language%253Den_US%2526release%253D214.17.3%2526instance%253DNA66&amp;site=https%3A%2F%2Fhelp.salesforce.com&amp;showSplash=true%27, %27Help%27, 1024, 768, %27width=1024,height=768,resizable=yes,toolbar=yes,status=yes,scrollbars=yes,menubar=yes,directories=no,location=yes,dependant=no%27, false, false);" title="Help for this Page (New Window)"><img src="/img/s.gif"  class="helpIcon" title="Help for this Page (New Window)"></a></div><div class="clearingBox"></div></div></div>

我尝试了这两种方法,但没有任何帮助。 chrome_options.add_argument('window-size=1920,1080') 和 capabilities = DesiredCapabilities.CHROME.copy()

你能帮忙吗? 提前致谢。

【问题讨论】:

如何Selenium 而不是WebDriver 结合WebDriverWait 定位元素的存在当元素根本不存在时? @DebanjanB 它是代码的一部分。它应该始终存在于页面中,否则它将出现超时异常并中断循环。我已经导入了 webdriver。 但我想你的 定位器策略 有点不对劲。你能用相关的 HTML 更新问题吗? @DebanjanB 我更新了我的问题。该代码实际上可以在没有“无头”设置的情况下工作。我认为可能与无头角色有关.. 【参考方案1】:

根据您共享的 HTML 和您的 代码试验 而不是使用 expected-conditions 方法 presence_of_element_located() 您需要使用方法visibility_of_element_located(),您可以使用以下任一解决方案:

CSS_SELECTOR:

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "select.title[name='fcf'][id$='_listSelect']")))

XPATH:

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//select[@class='title' and @name='fcf'][contains(@id,'_listSelect')]")))

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

【讨论】:

您好,感谢您检查问题。我仍然得到和以前一样的错误。它与真正的浏览器配合得很好。【参考方案2】:

对于任何寻求帮助的人,如何使用 WebDriverWait 在 Py Selenium 中无头化。

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")

driver = webdriver.Chrome(options=chrome_options). 
WebDriverWait(driver, ...)

options 关键字曾经是 chrome_options

【讨论】:

以上是关于Python-Selenium:Chrome 无头设置不适用于“WebDriverWait”的主要内容,如果未能解决你的问题,请参考以下文章

使用 chrome headless 和 selenium 下载

在docker容器中部署python-selenium+chrome-headless自动化脚本(续)

如何录制在 docker 内无头运行的 selenium 测试?

在无头模式下运行 Chrome

无头 Chrome 是不是支持策略配置?

无头Chrome驱动的分布式爬虫