Skip to content Skip to sidebar Skip to footer

How To Search, Arrow Down And Press Enter With Selenium

I'm trying to search for a company, arrow down and click enter on inhersight.com I have the following code but it doesn't seem to work: from selenium import webdriver from selenium

Solution 1:

To search for a company and click enter on inhersight.com instead of as the elements are Auto Suggestions so instead of arrow down you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument('start-maximized')
    options.add_argument('disable-infobars')
    options.add_argument('--disable-extensions')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.inhersight.com/companies")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".open-search.small-hide.margin-right-20.icon-36.icon-search.reverse.cursor-pointer"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Search women-rated companies']"))).send_keys("Apple")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[contains(@class,'select2-highlighted')]/div[@class='select2-result-label']/div[@class='weight-medium']"))).click()
    

Solution 2:

You could avoid the selections as the company name becomes part of the query string in the URL, with spaces replaced by "-" and all lower case. You can therefore direct .get on this formatted URL. You can add some handling in for if company not found.

from selenium import webdriver
company = 'Apple Federal Credit Union'# 'apple'
base = 'https://www.inhersight.com/company/' 
url = base + company.replace(' ', '-').lower()

d = webdriver.Chrome()
d.get(url)

#other stuff including handling of company not found (this text appears on the page so not hard)#d.quit()

Post a Comment for "How To Search, Arrow Down And Press Enter With Selenium"