Home > front end >  Selenium unable to locate though using expected conditions
Selenium unable to locate though using expected conditions

Time:01-21

Selenium is unable to locate the element by its xpath even though I'm using expected conditions to wait 3 seconds for element visibility. I'm trying to interact with the search bar, send a string, and press enter. My code is below:

def search_bar(self, search: str, xpath: str):
        """
        Looks for the search bar on the website and sends the search string

        Input: 
            search (str)
                the string to be search on
            xpath (str)
                the value of the xpath where the search is located
        

        """
        #search_bar = self.driver.find_element(by=By.XPATH, value=f'{xpath}')
        search_bar = self.wait.until(EC.element_to_be_clickable((By.XPATH, xpath )))
        search_bar.click()
        search_bar.send_keys(search)
        search_bar.send_keys(Keys.RETURN)

I think that it waits for the element to be clickable, then I can click it and send my search to it, (the website I am using is https://uk.trustpilot.com/).

scraper = Scraper()
scraper.load_webpage('https://uk.trustpilot.com/')
scraper.click_on_cookie('//*[@id="onetrust-accept-btn-handler"]')
scraper.search_bar('travel insurance', '//*[@id="heroSearchContainer"]')

Definition of my scraper class:

class Scraper:
    def __init__(self):
        """
       Defines the webdriver for the scraper class
        
        """
        _edge_driver = os.path.join(os.getcwd(), 'msedgedriver.exe') #path to the edge driver
        options = Edgeoptions()
        options.add_argument('start-maximized')
        self.driver = webdriver.Edge(executable_path=_edge_driver, options= options)
        self.wait = WebDriverWait(self.driver,3)

Finally by the error message which I am receiving is the following:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

CodePudding user response:

To send a character sequence to the search field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get('https://uk.trustpilot.com/')
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='query']"))).send_keys("Darman")
    
  • Browser Snapshot:

Darman

  • Note: 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
    
  • Related