Like logs onto a website using Python Selenium to the website each time changes the id, name, xpath selector?

I'm trying to use selenium to at least visit the site https://video.mosreg.ru/admin/#no-back-button there are dynamically changing xpath, selector, name, id, they can go in this case? Got first job as a programmer, it turned out that I was the only programmer, no one to ask, it is necessary to torment the forums. Log on to the website using the Selenium - only the first stage, then you will need to unload data from your 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")

# find by id
browser.find_element_by_id("oknzyun").send_keys("123") # input box of the login
browser.find_element_by_id("oknzyus").send_keys("123") # input password field
browser.find_element_by_id("oknzyv4").click()
March 23rd 20 at 19:01
5 answers
March 23rd 20 at 19:03
Solution
I told you, why did you create a new question then?

miller_Daugherty, at least, read about xpath. You are bound to id, and they change every time the xpath dynamically can not change. In your case it is an xpath. For example:
xpath forms login:
//*[@class="Wt-domRoot"]/div[2]/div/div/div/div/div[3]/div/div[2]/div/input[1]


the xpath forms of password:
//*[@class="Wt-domRoot"]/div[2]/div/div/div/div/div[3]/div/div[2]/div/input[2]


Button on this page is only one, for this xpath you can bind the type attribute of button:
//*[@type="button"]


There are about xpath in great detail and with examples.
Thank you very much, please tell me how to see the element xpath to look in chrome, get //*[@id="oqf0687"] - miller_Daugherty commented on March 23rd 20 at 19:06
@miller_Daugherty, chrome is not always correctly displayed, sometimes it really knits to the id element. Write is absolutely not difficult. Just look at the html tree and indicate transitions, for example.
PS If you help answers - mark solutions. And then two questions without solutions hanging already. - travis42 commented on March 23rd 20 at 19:09
Thank you very much for the help, if I understand this logic?
//*[@class="Wt-domRoot"]/div[2]/div/div/div/div/div[3]/div/div[2]/div/input[1]

1) looking for an element with a static class Wt-domRoot that it could move down
2) inside this diva choose the second
3) go down to div 4 below
4) at this level, select the third div
5) go down one div
6) at this level to select the second div
7) go down one div
8) at this level live DIVS and input select the first input - miller_Daugherty commented on March 23rd 20 at 19:12
@miller_Daugherty, Yes. Just as I wrote just above: it is "the way" a large number div'ω replaced by static objects, then the readability will be higher and if you change the structure of the pattern, the probability that the xpath will break will be lower. - travis42 commented on March 23rd 20 at 19:15
I understand, but here is the top static object is Wt-domRoot so we start with him? Further, I understand static fields, no. - miller_Daugherty commented on March 23rd 20 at 19:18
@miller_Daugherty, you can start even with the block forms "loginWindow radius-3 wrapper-gradient". To contact him you can use the design [contains(@class, "loginWindow")]. To completely specify all the value of the class do not need permit contains inaccurate entries. Then the xpath to the form of the username will be something like this:
//*[contains(@class, "loginWindow")]/div//input[1]

And password, respectively:
//*[contains(@class, "loginWindow")]/div//input[2]

I think the difference in readability is already evident, plus lower the probability that xpath will be invalid if the structure will change.
Although, I'm sure, if you disassemble, it is possible directly to access the forms, they are somewhere in any case should be static. - travis42 commented on March 23rd 20 at 19:21
can html to remove the site id and chrome will rebuild the path and become attached to other elements - kaya.Schmeler commented on March 23rd 20 at 19:24
March 23rd 20 at 19:05
see
visit just 4 of the input element. Two of them with the class "line-height-wide"

driver.find_elements_by_css_selector('input.line-height-wide') //will return two elements.

First name
The second is the password

Generally one button on the entire page
driver.find_element_by_css_selector('button').click()
March 23rd 20 at 19:07
Judging by the code front, this site is running on Wt, but because he deals with the mapping of id/name for form elements, pregenerate them on the backend. You need to look for in classes, according to this principle:
//div[contains(@class, 'loginWindow')]//input[contains(@class, 'line-height-wide')][1]

Please note that you are using the predicate function contains, i.e., searches for a substring and can appear superfluous coincidences, like loginWindow__title. There are still functions starts-with and ends-with others, not less useful things. Google in the direction of XPath. Two slashes means "somewhere among children at any depth"

I write in C#, you adapt 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 the same 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 beginner's problem: if ever in the process will encounter the iframe, then it must explicitly switch selectors, through the border will not work.
March 23rd 20 at 19:09
Add you can push off from a class/id, and from the words and types of items is a little more difficult to accept, but very difficult from the point of view changes on the website. Well, as you said, read xPath(much easier) the thing is extremely good and if you can manage(I understand that may sound difficult) simplify your life considerably.
Thank you very much, please tell me how to see the element xpath to look in chrome, get //*[@id="oqf0687"] - miller_Daugherty commented on March 23rd 20 at 19:12
@miller_DaughertyIf di is changed as you said, just go down the tree from top to bottom of the body element to the desired diva.

/html/body/div/div[2]/div/div/div[1]/div/div[3]/div/div[3]/div/button

What I did just deleted id all elements below the top of the button to the last diva in front of the body is logical touching only the top diva revealed. On chrome just gives me a straight xpath. Was barely a minute and the result obtained. =) - lea commented on March 23rd 20 at 19:15
March 23rd 20 at 19:11
Maybe I'm a little late with the Council. But the search fields for a login and password is much simpler:
login — //input[@type="text"]
password — //input[@type="password"]

Find more questions by tags SeleniumScrapyPython