4

The command that I use to click on an element in my script is:

mydriver.find_element(By.xpath("/html/body/div[1]/a/img")).click()

The execution of the script returns this error:

Traceback (most recent call last):
  File "click.py", line 12, in <module>
    mydriver.find_element(By.xpath("/html/body/div[1]/a/img")).click()
AttributeError: type object 'By' has no attribute 'xpath'

How could the problem be fixed ?

1
  • please do not forget to select the answer which helps you fix the problem. Commented Jun 1, 2016 at 19:01

2 Answers 2

3

Correct way of doing that in python is :

mydriver.find_element_by_xpath("/html/body/div[1]/a/img").click()
Sign up to request clarification or add additional context in comments.

Comments

3

You can also use the By class as in the original post, which is not an incorrect way of doing it in python. To do it this way, you must capitalize By.xpath to By.XPATH as shown in this stackoverflow post: Selenium Webdriver Python AttributeError type object has no attribute

I am not sure why it must be done this way, but it is working for me using python 3.6.4.

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.