简体   繁体   English

消息:元素在 Python Selenium 中不可交互

[英]Message: element not interactable in Python Selenium

I would like to click on load more button to show all 347 products, however it returns Message: element not interactable, any idea how can i fix it?我想单击加载更多按钮以显示所有 347 产品,但是它返回消息:元素不可交互,知道如何修复它吗?

load more button加载更多按钮

from selenium import webdriver
driver=webdriver.Chrome('e:/Users/fungc1/Documents/chromedriver.exe')
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
options=Options()


url= "https://www.toysrus.com.sg/lego"
driver.get(url)

while True:
    try:
                                        
        btn = driver.find_element_by_xpath("//*[name()='use' and @*='#chevron-circle-thin']")
        btn.click()
    except Exception as e:
        print(e)
        break

The thing is you would need to scroll till end of page to let selenium know where is the button in view point.问题是您需要滚动到页面末尾才能让 selenium 知道视点中的按钮在哪里。

Code:代码:

driver.get("https://www.toysrus.com.sg/lego/")
sleep(5)
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn[data-url*='www.toysrus.com']"))).click()
print("Clicked on ")

Update 1:更新1:

driver = webdriver.Chrome("C:\\Users\\***\\Desktop\\Selenium+Python\\chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://www.toysrus.com.sg/lego/")

while True:
    try:
        sleep(5)
        driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
        sleep(5)
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn[data-url*='www.toysrus.com']"))).click()
        print("Clicked on ")
    except Exception as e:
        print(e)
wait=WebDriverWait(driver, 10)

url= "https://www.toysrus.com.sg/lego"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".affirm.btn.btn-outline-secondary"))).click()
while True:
    try:
        wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='show-more']/button"))).click()
        time.sleep(5)
    except Exception as e:
        print(e)
        break

You should replace time.sleep() for the visibility of the spinner.您应该替换 time.sleep() 以获得微调器的可见性。 But just wait for the elements to load in.但只需等待元素加载。

allProducts=driver.find_elements_by_xpath("//div[@class='product-grid-wrapper']//div[@class='card product-tile product']")
print(len(allProducts))

Prints 346?打印 346?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM