Home > Back-end >  Selenium window scroll works only in Debug mode identifying Select element using Selenium and Python
Selenium window scroll works only in Debug mode identifying Select element using Selenium and Python

Time:12-23

I'm scraping a VUE.js website and when I have debug mode turned on in Selenium it can locate and click a drop down button but when I run it in normal mode it throws the following error message:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <select id="sortselectbox" data-ph-at-id="search-page-sort-drop-down"  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  tabindex="0">...</div>
  (Session info: headless chrome=96.0.4664.110)

Here's how I'm finding the dropdown button

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

Before that here's how I scroll to the top of the website so that the sortselectbox become visible for the driver

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

Here's the HTML element

<select id="sortselect" data-ph-at-id="search-page-sort-drop-down"  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>

I've tried adding more sleep before and after the scroll but it seems it's failing at that step. All signs are indicating that scrolling doesn't work in normal mode. Would I have to find another way locating that sortselectbox button without the use of the window.scrollTo script?

Thanks!

CodePudding user response:

This error message...

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <select id="sortselectbox" data-ph-at-id="search-page-sort-drop-down"  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  tabindex="0">...</div>

...implies that the click attempt to the element is obstructed by the chatBotNotification.

As the desired item is a Select element ideally to select an option you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using 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")
    
  • Using 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")
    

Note: You have to add the following imports:

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
  • Related