0

I am trying to automate the login to a "betting" website called Smarkets. I am using Selenium (with Python) to do this. I can get my code to navigate to the website "https://smarkets.com", click the little "Log in" button on the right hand top corner, and then to automatically enter my email and password in the pop up window that comes up. The next stage is for my code to automatically click the big "Log In" button on that pop up window, but I can't seem to be able to locate it. Inspecting the HTML elements for this button shows the following:

<button class = "micro-button -accented "  type = "submit">Log In</button>==$0

and nothing else - no ID, div class name, etc. From the code line above, I found the Xpath to be:

//*[@id="smarkets"]/div[3]/div/div/div[1]/form/button

So in my code, I tried to find this button via copying the Xpath above:

signInButton = browser.find_element_by_xpath("//*[@id="smarkets"]/div[3]/div/div/div[1]/form/button")

However when I run this code, I get an syntax error "invalid syntax". Please help - I spent hours trying to understand where I am going wrong...

3 Answers 3

3

I think your problem is with the quotes, can you try this?

signInButton = browser.find_element_by_xpath("""//*[@id="smarkets"]/div[3]/div/div/div[1]/form/button""")

The reason for a syntax error is always something in the side of the code, not a server or any other type of error.

The triple-quote in python allows you to format any (almost) character inside the string.

If you add an f you can include even variables

f"""Text text {your_variable}"""

more info here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much - it finally works! Can you just explain why you have enclosed it in two double quotes?
sure, updating my answer
3

It's always a best practice to use single quotes to wrap the attribute value in xpath and wrap the entire xpath in double quotes so that you don't get this kind of issues.

sample code:

plain xpath

//tag[@attribute='attribute value']

Using in code:

# when using above xpath in python code
driver.find_element_by_xpath("//tag[@attribute='attribute value']")

So in your case just change the line as below.

signInButton = browser.find_element_by_xpath("//*[@id='smarkets']/div[3]/div/div/div[1]/form/button")

If you really want to the double quotes in your xpath then you can use triple quotes to wrap the entire xpath as @Jose Angel Sanchez mentioned, but I don't see a reason why you want to complex the things.

Comments

0

Can you please try this code once its working i tried it

browser.find_element_by_xpath("//a[text()='Log in']").click();

Comments

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.