from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait
def main():
data = dict()
inpt = ['12FR010E-ND', 'CF1/4CT52R101J', 'MR40MB12.70R001JT', 'RNF18FTD1K00']
browser = webdriver.Firefox()
ip = "#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input"
rs = "#product-attributes > tbody > tr > td > div"
bt = "#header-search-button"
browser.get('https://www.digikey.cn/zh/products/category/resistors/2')
wait = WebDriverWait(browser, 10)
wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, ip)))
for s in inpt:
t_data = []
browser.switch_to.window(browser.current_window_handle)
browser.find_element_by_css_selector(ip).send_keys(s)
browser.find_element_by_css_selector(bt).click()
try:
wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, rs)))
result = browser.find_elements_by_css_selector(rs)
for r in result:
if r.text:
t_data.append(r.text)
data[s] = t_data
except Exception as e:
print(e)
print(data)
if __name__ == "__main__":
main()
|