Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

How to click on a button without ID using Python Selenium?

Basically, I am writing a script, that should open a specific URL on chrome, press certain buttons, login, press another button, and terminate chrome.

until now, it was pretty easy to do, because every button or field I needed had its own ID's.

Now, I have to press on a button, that does not have the ID and I am not sure how to do it.

enter image description here

here is my code.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pyautogui
import time
browser = webdriver.Chrome('XXX')
browser.get("XXX")

details_bttn = browser.find_element_by_id('details-button')
details_bttn.click()
proceed_bttn = browser.find_element_by_id('proceed-link')
proceed_bttn.click()
username = browser.find_element_by_id('login_name')
username.click()
username.send_keys('XXX')
password = browser.find_element_by_id('login_password')
password.click()
password.send_keys('XXX')
password.send_keys(Keys.ENTER)

I tried to do it with Xpath but it did not work, because the button only lit up, but it was not clicked. It is my first time working with python, so I definitely lack experience.

question from:https://stackoverflow.com/questions/66060590/how-to-click-on-a-button-without-id-using-python-selenium

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try with xpath

browser.find_element_by_xpath("//div[text() = 'System']")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...