Skip to content
Advertisement

clicking not working after pass text file in python selenium xpath for dropdown menu

I have a text file which contains all text for an dropdown list. I am trying to select text from the dropdown list those only exist in my text file. I tried this but didn’t work

  f = open('address.txt',encoding="utf-8")
  lines = f.readlines()
  for line in lines:
        y =line.strip()
        print(y)
        time.sleep(5)
        #here I am passing every line from my text file 
        region  = driver.find_element_by_xpath(f'//*[contains(text(),{y})]')
        #if I give text value instead of passing text file then it's working
        #region  = driver.find_element_by_xpath("//*[contains(text(),'Denver')]")  
        time.sleep(5)
        region.click()   

error massage:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=88.0.4324.104)  

Advertisement

Answer

You are missing the quotes defining the text (surrounding {y}) in your xpath. Try the following.

  f = open('address.txt',encoding="utf-8")
  lines = f.readlines()
  for line in lines:
        y =line.strip()
        print(y)
        time.sleep(5)
        #here I am passing every line from my text file 
        region  = driver.find_element_by_xpath(f'//*[contains(text(),"{y}")]')
        #if I give text value instead of passing text file then it's working
        #region  = driver.find_element_by_xpath("//*[contains(text(),'Denver')]")  
        time.sleep(5)
        region.click() 

Advertisement