繁体   English   中英

如何使用 selenium 单击按钮 - 网站

[英]How to click a button using selenium - website

我想在下面的代码中打开 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()

您得到的错误(但在您使用 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")))

刚刚测试了一下,效果很好。

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

find_element_by_xpath(xpath to element).click() 

您应该编写此代码,如下所示

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM