0

Please click on the link below to see the link "BEAUTY" on which I am clicking 1. I am using this code to click on the "Beauty" link

driver = webdriver.Chrome("C:\\Users\\gaurav\\Desktop\\chromedriver_win32\\chromedriver.exe")
driver.maximize_window()
driver.get("http://shop.davidjones.com.au")
object = driver.find_elements_by_name('topCategory')
  for ea in object:
  print ea.text
    if ea.text == 'Beauty':
      ea.click()
  1. I am getting the following exceptions after clickin on the link succesfully , can anybody tell me why I am getting it ?

        Traceback (most recent call last):
          File "C:/Users/gaurav/PycharmProjects/RIP_CURL/login_raw.py", line 10, in <module>  
        print ea.text  
          File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 73, in text  
        return self._execute(Command.GET_ELEMENT_TEXT)['value']  
          File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 493, in _execute  
        return self._parent.execute(command, params)  
          File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in execute  
        self.error_handler.check_response(response)  
          File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
        raise exception_class(message, screen, stacktrace)  
        selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document 
          (Session info: chrome=63.0.3239.132) 
          (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.2.9200 x86_64)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
gaurav tyagi
  • 25
  • 1
  • 10
  • I am trying to connect to the website to capture xpath. But all the elements appears to be stale. I guess the site should be protected from Selenium access – Swadhikar Jan 17 '18 at 06:17

3 Answers3

0

Try this:

from selenium import webdriver

print("bot started")

#chromeOptions = webdriver.ChromeOptions()

#driver = webdriver.Chrome(chrome_options=chromeOptions)

def specific_text(text, ea):
    return str(text) == ea.text

driver = webdriver.Chrome("C:\\Users\\gaurav\\Desktop\\chromedriver_win32\\chromedriver.exe")
driver.maximize_window()
driver.get("http://shop.davidjones.com.au")
object_ = driver.find_elements_by_name('topCategory')
text_headers = [str(specific_text('Beauty', ea)) for ea in object_]
#print(text_headers)
index_text = text_headers.index("True")
#print(index_text)
object_[index_text].click()
Julian Rachman
  • 575
  • 3
  • 15
  • thanks for your help , I tried what you said but it doesnt work and getiing the follwoing error-: `print ea.get_text(); AttributeError: 'WebElement' object has no attribute 'get_text'` – gaurav tyagi Jan 17 '18 at 04:46
  • i just edited my answer. try again. ill continue to help you the best i can. – Julian Rachman Jan 17 '18 at 04:49
  • Thank you so much, I have tried `ea.get_attribute('text')` as well but its giving _None_ value . – gaurav tyagi Jan 17 '18 at 05:22
  • Yep it's fixed now, no exception is coming , you are legend man, thanks a ton But I am still clueless about what were doing wrong earlier ? Why there were exceptions with our code ?? – gaurav tyagi Jan 17 '18 at 06:16
  • Because we were trying to do things that are not possible in the current libraries we were using. No problem man. If you think that my answer is all good feel free to accept it. – Julian Rachman Jan 17 '18 at 06:17
0

You need to take care of certain factors as follows :

  • You have tried to create a List by the name object. object is a reserved built-in symbol in most of the Programming Languages. So as per Best Programming Practices we shouldn't use the name object.
  • The line print ea.text is badly indented. You need to add indentation.
  • Once you invoke click() on the WebElement with text as Beauty you need to break out of the loop.
  • Here is your own working code with some minor tweaks :

    from selenium import webdriver
    
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe')
    driver.maximize_window()
    driver.get("http://shop.davidjones.com.au")
    object1 = driver.find_elements_by_name('topCategory')
    for ea in object1:
        print (ea.text)
        if ea.text == 'Beauty':
            ea.click()
            break
    
  • Console Output :

    Sale
    Brands
    Women
    Men
    Shoes
    Bags & Accessories
    Beauty
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

There's an easier way to do this. You can use an XPath that will specify the category name you want to click. That way you don't have to loop, it will find the desired element in one search.

//span[@name='topCategory'][.='Beauty']

I'm assuming you will be reusing this code. In cases like this, I would create a function that takes a string parameter which would be the category name that you want to click. You feed that parameter into the XPath above and you can then click any category on the page.

I tested this and it's working.

JeffC
  • 22,180
  • 5
  • 32
  • 55