无法单击按钮查看更多结果,无法单击异常元素

时间:2020-06-08 06:36:52

标签: python selenium web-scraping webdriver

我正在尝试从该网站https://easy.co.il/list/Shopping抓取一些数据,当您向下滚动业务列表时,当我尝试使用.click函数单击它时,它的末尾有一个按钮以查看更多结果。引发一个异常,即元素不难处理,我也尝试使用Keys.ENTER仍然是一个异常,我尝试使用此代码等待元素可单击。

results = driver.find_elements_by_xpath('//div[@class="biz-item "]')
print(len(results))

button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//button[@id="nextPageButton"]')))

len(结果)打印25,这是所有企业可见的信息,直到“查看更多结果”按钮为止。

我也尝试过定位按钮,该元素在页面上可见,但不能单击。

有人可以看看吗?谢谢!

2 个答案:

答案 0 :(得分:0)

点击之前,您需要scroll元素。使用location_once_scrolled_into_view滚动元素。

driver.get("https://easy.co.il/list/Shopping")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[@id="nextPageButton"]')))
button.location_once_scrolled_into_view
button.click()

如果仍然出现错误,请诱使Java脚本执行程序单击。

driver.get("https://easy.co.il/list/Shopping")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[@id="nextPageButton"]')))
button.location_once_scrolled_into_view
driver.execute_script("arguments[0].click();", button)

答案 1 :(得分:0)

使用.visibility_of_element_located方法,然后滚动:

button = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//button[@id="nextPageButton"]')))
button.location_once_scrolled_into_view
button.click()