[英]ElementNotInteractableException: element not interactable in Selenium
I am trying to get the review of a certain product but it returns an error.我正在尝试对某个产品进行评论,但它返回错误。
My code:我的代码:
import selenium
from selenium import webdriver
chrome_path = r"C:\Users\AV\AppData\Local\Programs\Python\Python39\Scripts\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://oldnavy.gapcanada.ca/browse/product.do?pid=647076053&cid=1180630&pcid=26190&vid=1&nav=meganav%3AWomen%3ADeals%3ASale&grid=pds_0_1034_1#pdp-page-content")
driver.execute_script("window.scrollTo(0, 1000)")
import time
from time import sleep
sleep(5)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.promoDrawer__handlebar__icon"))).click()
review = driver.find_elements_by_class_name("pr-rd-description-text")
for post in review:
print(post.text)
driver.find_element_by_xpath('//*[@id="pr-review-display"]/footer/div/div/a').click()
review2 = driver.find_elements_by_class_name("pr-rd-description-text")
for post in review2:
print(post.text)
It returns: selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable它返回:selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互
Can you please tell me what should I do?你能告诉我该怎么做吗?
That element is weird.那个元素很奇怪。 Even when I scroll into view, use actions to click it, or execute javascript to click, it doesn't work.
即使我滚动到视图中,使用操作单击它,或执行 javascript 进行单击,它也不起作用。 What I would suggest is just grabbing the
href
attribute from the element and going to that URL, using something like this:我建议只是从元素中获取
href
属性并使用以下内容转到 URL:
driver.get(driver.find_element_by_xpath('//*[@id="pr-review-display"]/footer/div/div/a').get_attribute('href'))
The button is really hard to click.这个按钮真的很难点击。
I guess it could be achieved by adding some more waits and moving with ActionChains
class methods.我想这可以通过添加更多等待并使用
ActionChains
class 方法来实现。
I could click it with Javascript code with no problems.我可以用 Javascript 代码毫无问题地单击它。 What it does:
它能做什么:
1 Scrolls to the Next button 1 滚动到下一步按钮
2 Clicks it. 2 单击它。
from selenium import webdriver
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
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get(
"https://oldnavy.gapcanada.ca/browse/product.do?pid=647076053&cid=1180630&pcid=26190&vid=1&nav=meganav%3AWomen%3ADeals%3ASale&grid=pds_0_1034_1#pdp-page-content")
driver.execute_script("window.scrollTo(0, 1000)")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.promoDrawer__handlebar__icon"))).click()
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".pr-rd-description-text")))
review = driver.find_elements_by_css_selector(".pr-rd-description-text")
for post in review:
print(post.text)
# wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".pr-rd-pagination-btn"))).click()
element = driver.find_element_by_css_selector(".pr-rd-pagination-btn")
# actions = ActionChains(driver)
# actions.move_to_element(element).click().perform()
driver.execute_script("arguments[0].scrollIntoView();", element) # Scrolls to the button
driver.execute_script("arguments[0].click();", element) # Clicks it
print("clicked next")
I also rearranged your code, moved imports to the beginning of the file, got rid of unpredictable time.sleep()
and used more reliable css locators.我还重新排列了您的代码,将导入移动到文件的开头,摆脱了不可预测的 time.sleep
time.sleep()
并使用了更可靠的 css 定位器。 However, your locator should also work.但是,您的定位器也应该可以工作。
I left the options I tried commented out.我把我试过的选项注释掉了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.