![](/img/trans.png)
[英]ElementClickInterceptedException: Message: element click intercepted Element is not clickable error clicking a radio button using Selenium and Python
[英]ElementClickInterceptedException: element click intercepted: Element is not clickable at point error clicking on Search button using Selenium Python
我想抓取以下網站: https://sprs.parl.gov.sg/search/home
但是當我的代碼點擊“搜索按鈕”時,我得到了錯誤:
ElementClickInterceptedException: element click intercepted: Element is not clickable at point (320, 1395)
我不確定是不是因為“搜索”按鈕不完全可見,需要向下滾動。 如果是這樣,我如何使用 Selenium 向下滾動頁面?
代碼試驗:
from bs4 import BeautifulSoup as bs
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path="C:/Users/chromedriver.exe")
page_url = 'https://sprs.parl.gov.sg/search/home'
driver.get(page_url)
# Get search box and fill it up
search = driver.find_element_by_css_selector('#divmpscreen2 > div.row > div:nth-child(1) > div > div:nth-child(1) > input')
search.send_keys('COS')
# This will select the 13th parliament
session = driver.find_element_by_xpath("//select/option[contains(text(), '13th')]")
session.click()
time.sleep(10)
# Find submit element and click
submit = driver.find_element_by_xpath("//button[@label='Search']/em")
submit.click()
要單擊Search按鈕,您需要為element_to_be_clickable()引入WebDriverWait ,您可以使用以下定位器策略:
driver.get('https://sprs.parl.gov.sg/search/home')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#divmpscreen2 > div.row > div:nth-child(1) > div > div:nth-child(1) > input"))).send_keys('COS')
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[text()='By Parliament']//following-sibling::select[1]")))).select_by_value('13: 13')
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-black' and @label='Search']/em"))))
瀏覽器快照:
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
這是 select 所需信息並單擊該按鈕的一種方法(編輯:沒有 ActionChains 的更好方法,並且也適用於較小尺寸的 windows):
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
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.support.ui import Select
import time as t
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
url = 'https://sprs.parl.gov.sg/search/home'
browser.get(url)
searchbox = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//label[contains(text(), 'By Keyword')]/following-sibling::input")))
print(searchbox.location_once_scrolled_into_view)
searchbox.send_keys('COS')
govt_nr = Select(WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//label[contains(text(), 'By Parliament')]/following-sibling::select"))))
govt_nr.select_by_index(13)
browser.execute_script("window.scrollTo(0,document.body.scrollHeight);")
t.sleep(1)
submit_button = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "button[label = 'Search']")))
submit_button.click()
設置是 linux/chromedriver,你只需要觀察導入,以及定義瀏覽器/驅動程序后的代碼。
Selenium 文檔: https://www.selenium.dev/documentation/
還要記住一件事:結果將在新選項卡中打開,因此您需要使用switch_to.window(driver.window_handles[-1])
的調子切換到它。
這是您假設使用 Selenium 單擊元素的方式:
def clickLastFoundElement(my_element):
try:
builder = Actions(browser)
builder.moveToElement(my_element).click().build().perform()
except ElementNotInteractableException:
try:
my_element.click()
except Exception:
hardClickElement(my_element)
def hardClickElement(element):
executor = (JavascriptExecutor)browser
executor.executeScript('arguments[0].click();', element)
有時一個元素不會被點擊,你必須通過注入 JavaScript 代碼來“硬”點擊它。
我們可以通過使用移動到元素或使用 java 腳本執行器來嘗試單擊
Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("path here"));
action.moveToElement(we).click().build().perform();
使用 javascript
WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.