[英]Can't click a button in python
大家下午好,我是新來做 Web 在 Python 中刮擦,如果您能幫我解決單擊按鈕時發生的這個問題,我將不勝感激(我已經能夠在其他人身上做到),下面是我的代碼:
opts = webdriver.ChromeOptions()
opts.add_argument("user-agent=Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36")
driver = webdriver.Chrome('C:/Users/JSALINA/Documents/chromedriver.exe', options=opts)
driver.maximize_window()
time.sleep(2)
driver.get('https://fasecolda.com/ramos/automoviles/historial-de-accidentes-de-vehiculos-asegurados/')
time.sleep(3)
driver.find_element(By.XPATH, '//*[@id="form"]/div/div/div/div[2]/div/div/div/div/span').click()
它顯示的錯誤如下:
我試圖點擊的按鈕是這樣的:
非常感謝您的友好合作。
干杯
該按鈕位於 iframe 中。 在嘗試定位並單擊它之前,等待該元素可單擊也是明智的。 下面的示例將起作用,設置是針對 linux,但您只需要觀察導入,並在定義瀏覽器后進行部分操作。
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.common.keys import Keys
import time as t
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
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://fasecolda.com/ramos/automoviles/historial-de-accidentes-de-vehiculos-asegurados/'
browser.get(url)
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@title='Términos']")))
t.sleep(3)
print('switched to iframe')
button = WebDriverWait(browser,5).until(EC.element_to_be_clickable((By.XPATH, '//*[text()="Acepto los Términos y Condiciones"]')))
print('located the button')
button.click()
print('clicked the button')
try:
button.click()
print('clicked the button again')
except Exception as e:
print('nevermind, click registered the first time')
出於某種原因,我不得不單擊它兩次才能注冊。
Selenium 文檔位於https://www.selenium.dev/documentation/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.