1

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.

Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26

2 Answers2

0

every html element in the website is addressable some way or another. the easiest is if the element has an id. which your element does not. the second easiest might be if the element has some unique text on it. this is mostly the case if it is a button or link.

in your case the link seems to have text in it so this might work

button = browser.find_element_by_link_text("System")

there are other methods to address elements. here is the complete list

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

https://selenium-python.readthedocs.io/locating-elements.html

https://selenium-python.readthedocs.io/api.html#locate-elements-by

usually you want to find by id first if that is possible (if it has an id). if not then find by link text (usually possible if it is a button or link). if it is a form element you can try by name. if all that fails you have to do by xpath or by css selector. xpath and css selectors are similar in power and complexity. use that with which you are more comfortable.

by tag name and by class name is seldom usefull since there are usually multiple unrelated elements with the same tag or the same class.

to give you more detailed help you need to post more of the html source code. as it is now this is all i can help you with.

Lesmana
  • 25,663
  • 9
  • 82
  • 87
0

Try with xpath

browser.find_element_by_xpath("//div[text() = 'System']")
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26