StaleElementReferenceException when trying to click on the links in a loop

前端 未结 3 1259
慢半拍i
慢半拍i 2021-01-26 10:54

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.Ch         


        
相关标签:
3条回答
  • 2021-01-26 11:30

    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.

    0 讨论(0)
  • 2021-01-26 11:39

    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()
    
    0 讨论(0)
  • 2021-01-26 11:49

    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
      
    0 讨论(0)
提交回复
热议问题