Search code examples
pythonweb-scrapingselectdrop-down-menuwebdriver

element not clickable from dropmenu


using selenium, i try de choose a specific value in a drop menu, but i have always an error

%reset -sf

site = 'https://www.mytek.tn/informatique/ordinateurs-portables/pc-portable.html'


driver.get(site)
sleep(1)

page_cat = requests.get(site)

tree_cat = html.fromstring(driver.page_source)

btn_all = tree_cat.xpath(".//option[@value='all']")
if len(btn_all) == 0:
    print("btn all dont exist")
else:
    print('choice all exist')

dropdown = Select(driver.find_element_by_id('limiter'))

dropdown.select_by_visible_text('Tous')

#dropdown.select_by_value('all')  # same error : ElementNotInteractableException

i've tried de see if selenium can read all the element in the drop menu : yes

   print("All selected options using ActionChains : \n")
    for opt in dropdown.options:
        print(opt.get_attribute('innerText'))
    time.sleep(5)

always same error

ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated

I'm going crazy

my imports:

#imports here
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains


import requests
import time
from time import sleep

from lxml import html
import logging as log
import pandas as pd

Solution

  • The drop down menu you trying to access appearing on the bottom of the page, not inside the initially visible screen.
    To access it with Selenium you need first to scroll to that element.
    Also, there are 2 selects there with similar locators while you need the second of them, so you should use corrected locator

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    
    actions = ActionChains(driver)
    wait = WebDriverWait(driver, 20)
    
    dropdown = wait.until(EC.presence_of_element_located((By.XPATH, "(//select[@id='limiter'])[last()]")))
    time.sleep(1)
    actions.move_to_element(dropdown).perform()
    time.sleep(0.5)
    dropdown = Select(driver.find_element_by_xpath("(//select[@id='limiter'])[last()]"))
    dropdown.select_by_visible_text('Tous')
    

    I hope this will work for you.