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?

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^
1
  • 2
    It is better to use relative xpath expression than absolute xpath expression. Commented Oct 14, 2016 at 9:00

Your Answer

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.