【问题标题】:How to click a button using selenium - website如何使用 selenium 单击按钮 - 网站
【发布时间】:2020-09-27 10:54:49
【问题描述】:

我想在下面的代码中打开 http 并单击显示烛台的图形按钮。 这是因为我想执行屏幕截图并保存标题的图形图像。 你能帮帮我吗?


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

PATH="C:\Program Files (x86)\chromedriver.exe"
driver= webdriver.Chrome(PATH)
driver.get("https://www.plus500.it/Instruments/TSLA")


try:
    element=WebDriverWait(driver,30).until(EC.element_to_be_clickable(By.ID, "ButtonLine"))
    element.click()

except:
    print('not found')
    driver.quit()

【问题讨论】:

  • 总是打印出你的异常,看看出了什么问题。做except Exception as e: print(e)
  • 我试过你的建议。结果是“消息:元素点击被拦截:元素 在点 (451, 624) 不可点击。其他元素会收到点击:

    ...

    (会话信息:chrome=85.0.4183.121)"
  • 表示我要点击的按钮不是按钮,是吗?我不明白为什么 python 说“其他元素会收到点击”。谢谢你的支持,我对这种语言真的很陌生
  • 已解决:添加了 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='button small cookie-button']")))。在单击 ButtonLine 之前单击()以删除 cookie 策略。谢谢

标签: python selenium button click


【解决方案1】:

您遇到的错误(但在您使用 try...except 时并未出现)是:

    Traceback (most recent call last):
  File "C:/Users/PycharmProjects/pythonProject3/main.py", line 14, in <module>
    element=WebDriverWait(driver,30).until(EC.element_to_be_clickable(By.ID, "ButtonLine"))
TypeError: __init__() takes 2 positional arguments but 3 were given

原因:你忘记了括号

修复:

element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.ID, "ButtonLine")))

刚刚测试过,效果很好。

【讨论】:

  • 好的,谢谢您的指正 =) 但是我遇到了这样的问题: 消息:元素点击被拦截:元素 在点 (451, 624) 是不可点击的。其他元素会收到点击:

    ...

    (Session info: chrome=85.0.4183.121)" – 它指的是什么?
  • 我在其他主题中看到可能是由于 coockie 政策的重叠接受弹出窗口(实际上在 html 页面中我发现“”在我的图表按钮的位置。我怎样才能接受或删除这个弹出窗口?
  • 已解决:添加了 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='button small cookie-button']")))。 click() 删除策略cookie,然后它就可以工作了。谢谢
【解决方案2】:

你可以像这样在.click()之后找到元素并添加:

find_element_by_xpath(xpath to element).click() 

【讨论】:

  • 谢谢,你能帮我写出正确的xpath吗?
  • 检查元素->找到按钮的html代码->右键->复制->复制完整的Xpath
【解决方案3】:

你应该编写如下代码

xpath = browser.find_element_by_xpath('TYPE THE XPATH')
xpath.click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-09
    • 2021-04-08
    • 2022-08-05
    • 2021-08-02
    • 2016-09-20
    • 1970-01-01
    • 2020-11-27
    • 2021-04-09
    相关资源
    最近更新 更多