简体   繁体   中英

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.

Appreciate the help, folks!

HTML code of the page: https://codeshare.io/2WxgOy Screenshot of the page:

png

For clicking on copy button which is available for download link, you may use the below xpath:

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

Similarly you may use the below Xpath for Link for forums

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

for the last and 4th button, you may use the below xpath:

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

for Paste you can use pyperclip

Make sure to install using pip pip install pyperclip

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

Update 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. So you can use your XPath something like this.

"//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

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