简体   繁体   中英

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. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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