1

I am writing a simple code where I needed to extract the contents of a html tag which was <span>. I've used the xpath to do it. Here's the error

File "C:\Users\BRS\Desktop\AccountChef\wattpad acc maker - Copy.py", line 41
    emailentry = wd.findElement(By.xpath(//*[@id='email']//span)).getText();
                                         ^
SyntaxError: invalid syntax

Since it's a syntax error, I don't think I need to give whole code. Please tell what i did wrong

2
  • 2
    A string must be in quotation marks. Commented Aug 3, 2020 at 19:36
  • 1
    As @DYZ mentioned, you need to wrap the parameter to By.xpath in quotes: "//*[@id='email']//span". Commented Aug 3, 2020 at 19:38

1 Answer 1

2

Seems there is a mixup of Selenium's and language binding syntax.

  • If you are using Python you need to use find_element_by_xpath() and text attribute as follows:

    emailentry = wd.find_element_by_xpath("//*[@id='email']//span").text
    
  • If you are using Java you need to use findElement() and getText() method as follows:

    String emailentry = wd.findElement(By.xpath("//*[@id='email']//span")).getText();
    
Sign up to request clarification or add additional context in comments.

1 Comment

@muzaafnameerfirdausi Checkout the updated answer and let me know the status.

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.