简体   繁体   English

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

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

I'd like to open the http inside the code below and click on the graph button that show the candlestick.我想在下面的代码中打开 http 并单击显示烛台的图形按钮。 This is because then I'd like to perfrom a screenshot and save the graph image of the title.这是因为我想执行屏幕截图并保存标题的图形图像。 Could you help me?你可以帮帮我吗?


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()

The error you were getting (but didn't appear as you wear using try...except) was :您得到的错误(但在您使用 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

Reason: You forgot a parenthesis原因:你忘记了括号

Fix:使固定:

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

Just tested it, it works fine.刚刚测试了一下,效果很好。

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

find_element_by_xpath(xpath to element).click() 

You should write this code as given below您应该编写此代码,如下所示

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