如何点击Tradingview上的“Tweet Image”按钮?

huangapple go评论131阅读模式
英文:

How do I click on the "Tweet Image" button on Tradingview?

问题

  1. # 以下是我的现有python代码:
  2. # 一些常量
  3. DRIVER_PATH = 'path_to_chromedriver'
  4. EMAIL = 'my_email_id'
  5. PWD = 'my_password'
  6. # ...
  7. # 在这里我卡住了

希望找到相机按钮的CSS路径。我尝试右键点击并检查它,但每次右键点击时,我都没有找到检查选项。

英文:

I am trying to access this website: Tradingview.com

I wrote a piece of Python code which uses selenium to do access it. After accessing the website, my goal is to click on the "Tweet Image" button. In order to do so, I need to first find the camera buttons' css path so that I can click on it. I cannot find the css path to the camera button.

Here is how it looks like:
如何点击Tradingview上的“Tweet Image”按钮?

This is my existing python code:

  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.chrome.service import Service
  4. from selenium.webdriver.chrome.options import Options
  5. from selenium.webdriver.common.keys import Keys
  6. from selenium.webdriver.common.action_chains import ActionChains
  7. from selenium.webdriver.support.wait import WebDriverWait
  8. from selenium.webdriver.support import expected_conditions as EC
  9. from selenium.webdriver.common.by import By
  10. # some constants
  11. DRIVER_PATH = 'path_to_chromedriver'
  12. EMAIL = 'my_email_id'
  13. PWD = 'my_password'
  14. class Browser:
  15. def __init__(self, driver: str, keep_open: bool) -> None:
  16. self.service = Service(driver)
  17. chrome_options = Options()
  18. chrome_options.add_experimental_option("detach", keep_open)
  19. chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
  20. self.driver = webdriver.Chrome(service=self.service, options=chrome_options)
  21. def open_page(self, url: str):
  22. self.driver.get(url)
  23. self.driver.maximize_window()
  24. def close(self):
  25. time.sleep(5)
  26. print("shutting down browser")
  27. self.driver.close()
  28. def sign_in(self):
  29. # open the sign in page
  30. self.open_page("https://www.tradingview.com/#signin")
  31. # click the email button
  32. button = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.NAME, "Email")))
  33. button.click()
  34. # enter email, password & click sign in
  35. self.driver.find_element(By.ID, "id_username").send_keys(EMAIL)
  36. self.driver.find_element(By.ID, "id_password").send_keys(PWD)
  37. sign_in_btn = self.driver.find_element(By.CSS_SELECTOR, ".submitButton-LQwxK8Bm.button-D4RPB3ZC.size-large-D4RPB3ZC.color-brand-D4RPB3ZC.variant-primary-D4RPB3ZC.stretch-D4RPB3ZC")
  38. sign_in_btn.click()
  39. def click_products_tab(self):
  40. products_tab = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Products")))
  41. products_tab.click()
  42. def click_tweet_image(self):
  43. tweet_image = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Tweet Image")))
  44. tweet_image.click()
  45. def save_chart_img(self):
  46. ActionChains(self.driver).key_down(Keys.ALT).send_keys('s').perform()
  47. element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Save image')))
  48. element.click()
  49. browser = Browser(DRIVER_PATH, True)
  50. # sign in
  51. browser.sign_in()
  52. # click on "Products" tab
  53. browser.click_products_tab()
  54. # click on "Tweet Image" button
  55. # I'm stuck here

I need help finding the css path of that camera button. I have tried right-clicking and inspecting it but everytime I right click, I don't get an option to inspect it.

答案1

得分: 1

  • 使用 CSS_SELECTOR
  1. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Take a snapshot'] > div#header-toolbar-screenshot"))).click()
  • 使用 XPATH
  1. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Take a snapshot']/div[@id='header-toolbar-screenshot']"))).click()
  • 注意:您需要添加以下导入语句:
  1. from selenium.webdriver.support.ui import WebDriverWait
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC
英文:

To click on the camera button element within TradingView website you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    1. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Take a snapshot'] > div#header-toolbar-screenshot"))).click()
  • Using XPATH:

    1. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Take a snapshot']/div[@id='header-toolbar-screenshot']"))).click()
  • Note: You have to add the following imports :

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

huangapple
  • 本文由 发表于 2023年7月13日 21:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定