1

I'my trying to apply an if / else statement in Python to know if I have an element or not and if it is the case, click on it. But I don't know why I have a syntax error and I don't see where I have made a mistake.

This is the part of my code concerned by this issue:

if driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/button[2]"):
 driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/button[2]".click()
 print "Oh this conversation already exists !"
 else:
 print "No conflict!

When I launch my script, I have the following syntax error:

> File "group_discussion_script.py", line 51 print "Oh this
> conversation already exists !
> ^ syntaxError: invalid syntax

I wonder if this line is really tolerated?

driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/button[2]".click()

Any idea whats wrong?

Sandeep
3332 gold badges5 silver badges21 bronze badges
asked Oct 14, 2016 at 6:43

1 Answer 1

1

Fixed, I have simply forgot to close the parenthesis:

driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/button[2]").click()
 HERE^
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Oct 14, 2016 at 7:03
1
  • 2
    It is better to use relative xpath expression than absolute xpath expression. Commented Oct 14, 2016 at 9:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.