社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

用selenium python单击按钮[复制]

joes • 6 年前 • 2507 次点击  

我是Python和Selenium的新手,我想点击 this url

我试过这个代码:

plus = driver.find_element_by_css_selector("button[class='b-btn b- 
ghost']")
plus.click()

但它不起作用,我得到这个错误:

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53857
文章 [ 2 ]  |  最新文章 6 年前
DebanjanB
Reply   •   1 楼
DebanjanB    7 年前

单击文本为的按钮 在这个范围内 url 网络驱动器 对于 可点击的元素

  • 使用 CSS_SELECTOR

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.b-btn.b-ghost"))).click()
    
  • 使用 XPATH :

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='b-btn b-ghost' and contains(., 'Afficher plus')]"))).click()
    
  • 注意 :必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
Matthewek
Reply   •   2 楼
Matthewek    7 年前

您试图单击的元素不可单击,或者可能重叠。

通过执行java脚本click函数,尝试单击指定的元素。

driver.execute_script("arguments[0].click();", element)

另一方面,您的页面可能尚未完全加载,因此元素可能还无法单击,您可以使用等待条件:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(By...)) //change selector

element.click();