简体   繁体   English

动态 ID - 如何在 Python 中单击带有 selenium 的动态按钮

[英]Dynamic ID - How to click a dynamic button with selenium in Python

the button is a copy one;该按钮是一个副本; that copies the file sharing link in the upper box.复制上方框中的文件共享链接。 Here's my attempt at it:这是我的尝试:

getLink = driver.find_element_by_xpath("""//*[starts-with(@id, "ic0") and contains(@id, "544")]""").click()

Still didn't work.仍然没有工作。 The class is '#c0-5443772' but it is dynamic. class 是“#c0-5443772”,但它是动态的。

Appreciate the help, folks!感谢帮助,伙计们!

HTML code of the page: https://codeshare.io/2WxgOy Screenshot of the page: HTML 页面代码: https://codeshare.io/2WxgOy页面截图:

png PNG

For clicking on copy button which is available for download link, you may use the below xpath:要单击可下载链接的复制按钮,您可以使用以下 xpath:

//h2[text()='Download Link']//following-sibling::button

Similarly you may use the below Xpath for Link for forums同样,您可以将以下 Xpath 用于论坛链接

//h2[text()='Link for forums']//following-sibling::button

for the last and 4th button, you may use the below xpath:对于最后一个和第四个按钮,您可以使用以下 xpath:

//h2[text()='Embed code']//following-sibling::button

for Paste you can use pyperclip对于粘贴,您可以使用 pyperclip

Make sure to install using pip pip install pyperclip确保使用 pip pip install pyperclip

paste_string= pc.paste();
  
print(paste_string)

Update 1:更新1:

from time import sleep
import tkinter as tk
import clipboard as clipboard
import pyautogui as pyautogui
import pyperclip as pyperclip
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd

driver = webdriver.Chrome("C:\\Users\\Desktop\\Selenium+Python\\chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 100)
driver.get("https://www.streamsb.com/login.html")

#   Login

user = driver.find_element_by_css_selector("#login > div > form > div:nth-child(3) > div > input[type=text]").send_keys("Enter here")
password = driver.find_element_by_css_selector("#login > div > form > div:nth-child(4) > div > input[type=password]").send_keys("Enter here")
enter = driver.find_element_by_css_selector("#login > div > form > div:nth-child(7) > div > input[type=submit]").click()
#   Navigation and upload
driver.find_element_by_css_selector("#mAcct > div.mnu > div > ul > li:nth-child(1) > a").click()
driver.find_element_by_css_selector("#mainmenu > ul > li:nth-child(2) > a").click()
driver.find_element_by_css_selector("#filepc").send_keys("C:\\Selenium+Python\\file_example_MP4_480_1_5MG.mp4")

uploadButton = driver.find_element_by_class_name("upload-form-button").click()

copy_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.btndiv')))

print("before clicking copy button.")
copy_button.click()

text = clipboard.paste() # text will have the content of clipboard
print(text)

there are four Copy Html codes for 4 tabs and each has a different id but the same class. 4 个标签有四个副本 Html 代码,每个标签都有不同的 id,但 class 相同。 So you can use your XPath something like this.所以你可以使用你的 XPath 这样的东西。

"//h2[contains(.,'"+tabName+"')]//following-sibling::button"

Here tab Name is the Variable that would pass as the tab Names value eg.这里选项卡名称是作为选项卡名称值传递的变量,例如。

//h2[contains(.,'Download Link')]//following-sibling::button

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

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