How to first write in search box and then select the first index in Listview by Selenium in Python

Question:

I am going to scrap glassdoor review for a list of companies. To do this, I need to first login in order to get access all reviews. Then, my approach is entering the name of the 1st company and then scarp its review…..do the same for all companies in the list….
To do this when I go to this URL "https://www.glassdoor.co.in/member/home/index.htm", I should insert the name of company in "search text-box" and then select first index from list view and finally enter search button to go the next link which is review of that comapny..My challenge is with selecting 1st index from "search textbox".. Actually, the last line of code where I going to send cursor on "search button" . I have this error "AttributeError: ‘NoneType’ object has no attribute ‘send_keys’"

I would appreciate if you could help me!

I used the following code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, WebDriverException
from selenium.webdriver.chrome.options import Options
import time
import pandas as pd  

driver_path= r"C:UsersTMaghsoudiDesktopchromedriver_win32.exe"

# chrome options
options = webdriver.ChromeOptions()
# options.add_argument("--start-maximized")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_experimental_option('excludeSwitches', ['enable-logging'])


driver = webdriver.Chrome(driver_path, chrome_options=options)

# set driver
driver = webdriver.Chrome(driver_path, chrome_options=options)

# get url
url = "https://www.glassdoor.co.in/Job/index.htm"
driver.get(url)

time.sleep(3)
driver.find_element(By.CLASS_NAME, "HeaderStyles__signInButton").click()
# singin = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "SignInButton")))
# singin.click()

time.sleep(5)

Enter_email= driver.find_element(By.ID, "modalUserEmail")
Enter_email.send_keys("XXXXXX")
Enter_email.send_keys(Keys.ENTER)
Enter_pass= driver.find_element(By.ID,"modalUserPassword")
Enter_pass.send_keys("XXXXX")

SingIn= WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@class='d-flex align-items-center flex-column']/button[@class='gd-ui-button mt-std minWidthBtn css-1dqhu4c evpplnh0']")))
SingIn.click()

time.sleep(5)
driver.set_window_size(1120, 1000)
driver.find_element(By.CLASS_NAME,"siteHeader__HeaderStyles__navigationItem:nth-child(2)").click()

Company=driver.find_element(By.ID,"sc.keyword").send_keys("Amazon")
Company.send_keys(Keys.ENTER)  **#the error is here!!!!!!**
Asked By: Tahereh Maghsoudi

||

Answers:

By running

Company = driver.find_element(By.ID,"sc.keyword").send_keys("Amazon")

you are assigning the value None to Company, this is because .send_keys() returns the value None if it executed without errors.

So you have to run

Company=driver.find_element(By.ID,"sc.keyword")
Company.send_keys("Amazon")
Company.send_keys(Keys.ENTER)

You can also run the last two commands in one line

Company.send_keys("Amazon" + Keys.ENTER)
Answered By: sound wave

To first write within the search box and then instead of selecting the first index in Listview, you could send ENTER back to back using the following locator strategies:

  • Code block:

    from selenium.webdriver.common.keys import Keys
    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://www.glassdoor.co.in/Job/index.htm')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='d-none d-lg-block']//button[text()='Sign In']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#modalUserEmail"))).send_keys("[email protected]")
    driver.find_element(By.CSS_SELECTOR, "input#modalUserPassword").send_keys("dev_anjan")
    driver.find_element(By.XPATH, "//span[text()='Sign In']").click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[aria-label='Search Keyword']"))).send_keys("Amazon" + Keys.RETURN)
    
  • Browser Snapshot:

glassdoor_Amazon

Answered By: undetected Selenium