P
P
Pavel2020-06-05 14:56:11
Python
Pavel, 2020-06-05 14:56:11

Python Selenium how to make click on form button?

Good
afternoon Need help from professionals
I can't click on a button in a form.
Also, it is not possible to set data in the form or click on the checkbox on the form (VIM).
It seems to me that the form is generated through a JS script, which is probably why everything is not so simple.

My code

import os
from selenium import webdriver
from time import sleep

URL = 'https://avtokod.mos.ru/Autohistory#!/Home'

draver = webdriver.Chrome(executable_path=os.path.abspath('chromedriver'))

draver.get(URL)
draver.find_element_by_class_name('npClose').click()
sleep(2)
draver.find_element_by_xpath("//button-ui[@class='hydrated']").click()


5eda322411ac0590563569.png

Thanks in advance for any help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-06-05
@velllum

It seems to me that the form is generated through a JS script, which is probably why everything is not so simple

Selenium just knows how to do JS.
The point here is that the form code is in the shadow-root (the same will happen with the iframe). Therefore, you need to do it a little differently, namely, execute the JS code using driver.execute_script, which will select the necessary elements through JS selectors.
ps: maybe shitcode, with iframe it seems like I can work correctly, through switch_to (), with shadow root it won’t work like that.
import os
from selenium import webdriver
from time import sleep


URL = 'https://avtokod.mos.ru/Autohistory#!/Home'
driver = webdriver.Chrome(executable_path=os.path.abspath('chromedriver'))
driver.get(URL)
sleep(3)


# Переключение на VIN
driver.execute_script("return document.querySelector('autohistory-card').shadowRoot.querySelector('label[for=autoVin]')").click()


# Поле ввода VIN
vin_input = driver.execute_script("return document.querySelector('autohistory-card').shadowRoot.querySelector('input#carVin')")
vin_input.send_keys('1234567890')


# Поле ввода СТС
sts_input = driver.execute_script("return document.querySelector('autohistory-card').shadowRoot.querySelector('input#carSts')")
sts_input.send_keys('1234567890')


# Кнопка проверить. Она лежит в еще одном shadow-root
driver.execute_script("document.querySelector('autohistory-card').shadowRoot.querySelector('button-ui').shadowRoot.querySelector('button').click()")

sleep(10)
driver.quit()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question