2

How to address the error AttributeError: 'NoneType' object has no attribute 'click'? Its failing in self.home.get_you_button().click(). It’s working fine when I am not creating Page Object Class...it clicks on the You button without any error but by using POM it’s failing. The url is https://huew.co/

Code trials:

from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

class HomePage():

    def __init__(self,driver):
        self.driver = driver

    def wait_for_home_page_to_load(self):
        wait =WebDriverWait(self.driver,30)
        wait.until(expected_conditions.visibility_of(self.driver.find_element_by_tag_name('html')))

    def get_you_button(self):

        try:
            element = self.driver.find_element_by_xpath("//div[@class='desktop-public-header']/a[@ng-controller='UserNavigationInteractionCtrl'][6]")

        except:
            return None
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Avinav Das
  • 71
  • 1
  • 2
  • 3
  • So what's the `Exception` you are catching? – Fynn Becker Feb 02 '19 at 23:06
  • Can you post your html? – KunduK Feb 02 '19 at 23:11
  • 2
    Your `get_you_button()` function explicitly returns `None` in the case of **any** `Exception`. Is it catching one? If so, which one? This should really be included in the question. – Fynn Becker Feb 02 '19 at 23:16
  • @FynnBecker if element not found it will return None the except block is handling this but here the problem is with Click – Avinav Das Feb 02 '19 at 23:17
  • 3
    So what do you expect to happen when that function returns `None` and you attempt to call `.click()` on `None`? That would indeed raise the `AttributeError` you are encountering. Did you debug your code to ensure that this is not what's happening? Again, all this information should be included in the question. – Fynn Becker Feb 02 '19 at 23:21
  • 1
    @AvinavDas Even if no exception was raised, your `get_you_button` method doesn't return anything explicitly, which means it will still return `None`. – Unatiel Feb 02 '19 at 23:25
  • Add the relevant code... you keep referring to a click but there is no click in the code you posted. Get rid of the `try-except` and `return self.driver.find_element_by_xpath(...)` and you'll find your error. – JeffC Feb 03 '19 at 00:37
  • https://huew.co/ I want to perform click on 'You' button for the above website. Which locator should I use, I tried with driver.find_element_by_xpath("//div[@class='desktop-public-header']/a[@ng-controller='UserNavigationInteractionCtrl'][6]") but its throwing error- Nonetype Object has no attribute, means its not identifying the element. This xpath is working if I am not calling this method from Page Object Class (where I stored my web elements) – Avinav Das Feb 03 '19 at 07:33
  • Also tried with element = self.driver.find_element_by_xpath(("//div[@class='desktop-menu-icon-text']")[4]) still click operation is not performed – Avinav Das Feb 03 '19 at 08:15
  • Possible duplicate of [Don't understand what this AttributeError means](https://stackoverflow.com/questions/8949252/dont-understand-what-this-attributeerror-means) – tripleee Feb 04 '19 at 08:00

1 Answers1

5

This error message...

AttributeError: 'NoneType' object has no attribute 'click'

...implies that no element was returned by WebDriverWait so None was returned from the except block which have no attribute as 'click'.

As your usecase is to click on the element with text as You a couple of facts:

  • You don't need to wait for home page to load with WebDriverWait seperately. So you can remove the method wait_for_home_page_to_load(self).
  • Instead induce once you invoke get() for the url https://huew.co/ induce WebDriverWait for the desired element i.e. element with text as You to be clickable.
  • It will be better to catch the actual exception TimeoutException
  • Not sure about your usecase but there is no point returning None rather print the relevant text and break.
  • You can use the following solution:

    self.driver = driver
    try:
        return (WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class= 'desktop-menu-container ng-scope' and @href='/profile/']"))))
        print("YOU link found and returned")
    except TimeoutException:
        print("YOU link not found ... breaking out")
        break
    
  • You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352