Citi
Citi

Reputation: 139

python using selenium webdriver mouser

I'm trying to open the Mouser website and use the search bar to send some data. Here's an example of the code but I can't get the right CSS selector. Thank you.

import time
from openpyxl import load_workbook
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path='C:/Users/amuri/AppData/Local/Microsoft/WindowsApps/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/site-packages/chromedriver.exe')
driver.implicitly_wait(1)

url ='https://www.mouser.com/'
driver.get(url)
print(driver.title)
wait = WebDriverWait(driver, timeout=1)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#as-input-066 .form-control")))
elem = driver.find_element_by_css_selector("#as-input-066 .form-control")
elem.click()
elem.send_keys("myString")

Upvotes: 1

Views: 106

Answers (1)

vitaliis
vitaliis

Reputation: 4212

Try the following css:

.form-control.headerSearchBox.search-input.js-search-autosuggest.as-input

xpath is even shorter:

//input[contains(@id,'as-input')]

Explanation: it looks at id that contains as-input

One more suggestion: Change

wait = WebDriverWait(driver, timeout=1)

to

wait = WebDriverWait(driver, timeout=15)

1 second is too small timeout. It should be at least 10.

Upvotes: 1

Related Questions