Skip to main content
 首页 » 编程设计

python之单击()在 Selenium 中不起作用

2025年05月04日28shanyou

我目前正在使用 selenium 和 python,我的网络驱动程序是 firefox 我尝试了点击事件,但它不起作用

网站 = www.cloudsightapi.com/api

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
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.common.exceptions import TimeoutException 
import time 
from selenium.webdriver.common.action_chains import ActionChains 
 
import os 
driver = webdriver.Firefox() 
driver.get("http://cloudsightapi.com/api") 
 
wait = WebDriverWait(driver, 10) 
element = wait.until(EC.element_to_be_clickable((By.ID, "dropzoneTarget"))) 
element.click() 

请帮忙!!

请您参考如下方法:

点击 via javascript 对我有用:

element = wait.until(EC.element_to_be_clickable((By.ID, "dropzoneTarget"))) 
driver.execute_script("arguments[0].click();", element) 

现在,另一个问题是单击该元素只会给您带来更多麻烦。将打开一个文件上传弹出窗口。而且,问题是,您无法通过 selenium 控制它。

解决该问题的一种常见方法是找到文件输入并将其值设置为您要上传的文件的绝对路径,请参阅:

在您的情况下,输入 是隐藏的,使其可见 并将路径发送给它:

element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input.dz-hidden-input[type=file]"))) 
 
# make the input visible 
driver.execute_script("arguments[0].style = {};", element) 
element.send_keys("/absolute/path/to/image.jpg")