2

I am trying to locate an element that has the following line in the chrome inspect code, <href="app/arp/home/profile">.

My line is:

driver.find_element(By.xpath("//a[@href='/app/arp/home/profile']")).click()

But I get the following error:

AttributeError: type object 'By' has no attribute 'xpath'

What is wrong?

1
  • How about trying driver.find_element_by_xpath("//a[@href='/app/arp/home/profile']") Commented Jan 6, 2021 at 7:23

3 Answers 3

3

Till Selenium v3.141.0 to locate an element using you can use the following syntax:

driver.find_element_by_xpath("//a[@href='/app/arp/home/profile']")

However, in the upcoming releases find_element_by_* commands will be deprecated

def find_element_by_xpath(self, xpath):
    """
    Finds an element by xpath.

    :Args:
     - xpath - The xpath locator of the element to find.

    :Returns:
     - WebElement - the element if it was found

    :Raises:
     - NoSuchElementException - if the element wasn't found

    :Usage:
        ::

            element = driver.find_element_by_xpath('//div/td[1]')
    """
    warnings.warn("find_element_by_* commands are deprecated. Please use find_element() instead")
    return self.find_element(by=By.XPATH, value=xpath)
        

From Selenium v4.x onwards the effective syntax will be:

driver.find_element(By.XPATH, "//a[@href='/app/arp/home/profile']")

An example:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    driver.get("https://www.google.com/")
    element = driver.find_element(By.NAME, "q")
    print(element)
    driver.quit()
    
  • Console Output:

    <selenium.webdriver.remote.webelement.WebElement (session="04a9fac269c3a9cb724cc72769aed4e0", element="1b8ee8d0-b26a-4c67-be10-615286a4d427")>
    
Sign up to request clarification or add additional context in comments.

2 Comments

This is news to me!!! I've been using the By class mainly in Java what is the reason for the deprecation?
@MosheSlavin Give me some time, let me try to pull out the discussions.
1

Please try this one

driver.find_element_by_xpath("//a[@href='/app/arp/home/profile']")

Comments

1

AttributeError: type object 'By' has no attribute 'xpath'.

This means you didn't import the correct object By.

Make sure to add the import on the top of the page:

from selenium.webdriver.common.by import By 

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.