A
A
ArtiomK2019-06-04 11:59:25
Python
ArtiomK, 2019-06-04 11:59:25

How to login to the site using Python Selenium, the site changes id, name, xpath, selector every time?

I am now trying to use selenium to at least go to the site https://video.mosreg.ru/admin/#no-back-button , xpath, selector, name, id dynamically change there, can I somehow go in this case? I got my first job as a programmer, it turned out that I was the only programmer here, there was no one to ask, I had to torment the forums. Login to the site using Selenium is only the first stage, then you will need to upload the data of internal pages. =(

import os
from selenium import webdriver
driver_path = os.path.join("C:\\", "Users", "kozyrev.av", "Desktop", "chromedriver_win32", "chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_argument('window-size=2028x900')
browser = webdriver.Chrome(executable_path=driver_path, options=options)
browser.get("https://video.mosreg.ru/admin/#no-back-button")

# поиск по id
browser.find_element_by_id("oknzyun").send_keys("123")  # input поле логина
browser.find_element_by_id("oknzyus").send_keys("123")  # input поле пароля
browser.find_element_by_id("oknzyv4").click()

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
Ivan Yakushenko, 2019-06-04
@ArtiomK

I answered you, why are you creating a new question?
ArtiomK, at least read about xpath. You bind to id, and they change every time, xpath cannot change dynamically. In your case, you need exactly xpath. For example:
xpath of the login form:
password form xpath:
There is only one button on this page, which xpath can be bound by the type button attribute:
Here about xpath in great detail and with examples.

D
Dmitry Eremin, 2019-06-04
@EreminD

see
there are only 4 input elements on the page. Of these, two with the class "line-height-wide"
The first is the name
The second is the password
The button is generally one on the entire page

driver.find_element_by_css_selector('button').click()

F
Fat Lorrie, 2019-06-04
@Free_ze

Judging by the front code, this site is powered by Wt , and therefore does the id / name mapping for the form elements itself, pre-generating them on the backend. You need to search by class, according to this principle:
I will write in C #, you adapt to your python:

const string loginFormPath = "//div[contains(@class, 'loginWindow')]";
const string inputFieldSelector = "input[contains(@class, 'line-height-wide')]";

const string nameFieldPath = loginFormPath + "//" + inputFieldSelector + "[1]";
const string passwordFieldPath = loginFormPath + "//" + inputFieldSelector + "[2]";

Or similar with CSS selectors
const string loginFormPath = ".loginWindow";
const string inputFieldSelector = "input.line-height-wide";

const string nameFieldPath = loginFormPath + " " + inputFieldSelector + ":nth-child(1)";
const string passwordFieldPath = loginFormPath + " " + inputFieldSelector + ":nth-child(2)";

PS The most popular newbie problem: if you ever meet in the process of work iframe, then you need to explicitly switch to it, selectors across this border will not work.

A
Andrey_Dolg, 2019-06-04
@Andrey_Dolg

I will add that you can start not from the class / id, but from the words and types of elements, this is a little more difficult to perceive, but also very difficult in terms of changing on the site. Well, as you were told, read xPath (much easier) is an extremely good thing, and if you master it (I understand that it may seem difficult), simplify your life by an order of magnitude.

A
Alexey, 2019-06-20
@Toximiner

Maybe I'm a little late with the advice. But searching for fields to enter a login and password is much simpler:
login - //input[@type="text"]
password - //input[@type="password"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question