无法从下拉菜单中单击的元素

element not clickable from dropmenu

使用 selenium,我尝试在下拉菜单中选择特定值, 但我总是出错

%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

我试过看看 selenium 是否可以读取下拉菜单中的所有元素:是的

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

总是同样的错误

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

我要疯了

我的进口商品:

#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

您尝试访问的下拉菜单显示在页面底部,而不是在最初可见的屏幕内。
要使用 Selenium 访问它,您需要先滚动到该元素。
此外,那里有 2 个 select 具有相似的定位器,而您需要其中的第二个,因此您应该使用更正后的定位器

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')

我希望这对你有用。