Selenium window 滚动仅在调试模式下使用 Selenium 和 Python 识别 Select 元素

Selenium window scroll works only in Debug mode identifying Select element using Selenium and Python

我正在抓取一个 VUE.js 网站,当我在 Selenium 中打开调试模式时,它可以找到并单击下拉按钮,但是当我 运行 它在正常模式下会抛出以下错误消息:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <select id="sortselectbox" data-ph-at-id="search-page-sort-drop-down" class="form-control au-target" value.bind="searchParams.sortBy" change.delegate="sortfilterSearch()" tabindex="0" data-ph-id="ph-page-element-page20-4VGGDW" au-target-id="169">...</select> is not clickable at point (707, 444). Other element would receive the click: <div class="chatBotNotificationText" tabindex="0">...</div>
  (Session info: headless chrome=96.0.4664.110)

这是我找到下拉按钮的方法

Order = driver.find_element_by_xpath("//*[@id='sortselectbox']")

在此之前,我如何滚动到网站顶部,以便 sortselectbox 对 driver

可见
driver.execute_script("window.scrollTo(0, 220)") #Page up

这是 HTML 元素

<select id="sortselect" data-ph-at-id="search-page-sort-drop-down" class="form-control au-target" value.bind="searchParams.sortBy" change.delegate="sortfilterSearch()" tabindex="0" data-ph-id="ph-page-element-page20-UCZFWs" au-target-id="150"> <option value="Most relevant" key="c-internal-digital-technology-it-53pxnB-ph-search-results-v2-view4-mostRelevantText" data-ph-id="ph-page-element-page20-srcQGN"> Most relevant </option> <option value="Most recent" key="c-internal-digital-technology-it-53pxnB-ph-search-results-v2-view4-mostRecentText" data-ph-id="ph-page-element-page20-Br2Xo6"> Most recent </option> </select>

我试过在滚动前后增加睡眠时间,但似乎在那一步失败了。所有迹象都表明滚动在正常模式下不起作用。我是否必须找到另一种方法来定位 sortselectbox 按钮而不使用 window.scrollTo 脚本?

谢谢!

这个错误信息...

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <select id="sortselectbox" data-ph-at-id="search-page-sort-drop-down" class="form-control au-target" value.bind="searchParams.sortBy" change.delegate="sortfilterSearch()" tabindex="0" data-ph-id="ph-page-element-page20-4VGGDW" au-target-id="169">...</select> is not clickable at point (707, 444). Other element would receive the click: <div class="chatBotNotificationText" tabindex="0">...</div>

...表示对 元素的点击尝试被 chatBotNotification.

阻止

因为想要的商品是 Select element ideally to select an option you need to induce WebDriverWait for the and you can use either of the following :

  • 使用CSS_SELECTOR:

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#sortselect[data-ph-at-id='search-page-sort-drop-down']")))).select_by_value("Most relevant")
    
  • 使用 XPATH:

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='sortselect' and @data-ph-at-id='search-page-sort-drop-down']")))).select_by_value("Most recent")
    

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

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