M
M
Michael2018-02-27 12:17:56
Python
Michael, 2018-02-27 12:17:56

Python Selenium work with find_element_by_xpath check?

Hello, I just started getting acquainted with Selenium, read the docks on the site and could not find the design I needed.
Task: the script goes to the target page and searches, if it finds it clicks (if not, it breaks)

driver.find_element_by_xpath(u"//img[@alt='Спасибо']").click()

I need to implement an occurrence check construct, because at the moment the script just gets up and stops working when it does not find the "Thanks" button, and would execute driver.back ()
I tried to implement it using if but in this case the script refused to work correctly at all. As I understand it, selenium itself has its own special construction, the syntax of which must be observed.
Thanks in advance for any hints.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2018-02-27
@moonz

Found the solution myself =_=

try:
                driver.find_element_by_xpath(u"//img[@alt='Спасибо']").click()
                driver.back() 
except:
                driver.back()

S
Stanislav Pugachev, 2018-02-27
@Stqs

moonz ,
tyts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question