Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ElementNotInteractiableException:元素在Selenium中不可交互_Python_Selenium_Selenium Webdriver_Web Scraping_Selenium Chromedriver - Fatal编程技术网

Python ElementNotInteractiableException:元素在Selenium中不可交互

Python ElementNotInteractiableException:元素在Selenium中不可交互,python,selenium,selenium-webdriver,web-scraping,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Web Scraping,Selenium Chromedriver,我试图获得某个产品的评论,但它返回了一个错误 我的代码: 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://oldnav

我试图获得某个产品的评论,但它返回了一个错误

我的代码:

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)
它返回:selenium.common.exceptions.elementnotinteractiableexception:Message:element不可交互


你能告诉我该怎么办吗?

那个元素很奇怪。即使当我滚动到视图中,使用操作来单击它,或者执行javascript来单击它时,它也不起作用。我建议您只需从元素中抓取
href
属性并转到该URL,如下所示:

driver.get(driver.find_element_by_xpath('//*[@id="pr-review-display"]/footer/div/div/a').get_attribute('href'))

这个元素很奇怪。即使当我滚动到视图中,使用操作来单击它,或者执行javascript来单击它时,它也不起作用。我建议您只需从元素中抓取
href
属性并转到该URL,如下所示:

driver.get(driver.find_element_by_xpath('//*[@id="pr-review-display"]/footer/div/div/a').get_attribute('href'))

这个按钮真的很难点击

我想这可以通过添加更多的等待和使用
ActionChains
类方法来实现

我可以用Javascript代码点击它,没有问题。 它的作用是:

1滚动到下一个按钮

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")
我还重新整理了您的代码,将导入移到了文件的开头,消除了不可预知的
time.sleep()
,并使用了更可靠的css定位器。但是,您的定位器也应该工作


我留下了我尝试过的选项。按钮很难点击

我想这可以通过添加更多的等待和使用
ActionChains
类方法来实现

我可以用Javascript代码点击它,没有问题。 它的作用是:

1滚动到下一个按钮

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")
我还重新整理了您的代码,将导入移到了文件的开头,消除了不可预知的
time.sleep()
,并使用了更可靠的css定位器。但是,您的定位器也应该工作


我留下了我尝试过的选项。

奇怪。它对我不起作用,如果我尝试元素,我仍然会得到
元素不可交互
错误。单击(),运行代码实际上不会单击按钮。@C.Peck运行代码时是否看到任何错误?我只是再次检查了三次,然后按下了按钮。如果您检查分页,就可以验证它。它对我不起作用,如果我尝试元素,我仍然会得到
元素不可交互
错误。单击(),运行代码实际上不会单击按钮。@C.Peck运行代码时是否看到任何错误?我只是再次检查了三次,然后按下了按钮。如果您检查分页,您可以验证它。这是一个很好的选择这是一个很好的选择