把等待也加入到try里: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():
browser = webdriver.Chrome(r'C:\Users\sesa395799\Downloads\chromedriver_win32\chromedriver.exe')
ip = "#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input"
rs = "#product-attributes > tbody > tr > td > div"
browser.get('https://www.digikey.cn/zh/products/category/resistors/2')
wait = WebDriverWait(browser, 30)
wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, ip)))
for s in ['12FR010E-ND\n', 'CF1/4CT52R101J\n', 'MR40MB12.70R001JT\n', 'RNF18FTD1K00\n']:
browser.switch_to.window(browser.current_window_handle)
element = browser.find_element_by_css_selector(ip)
element.send_keys(s)
try:
wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, rs)))
result = browser.find_elements_by_css_selector(rs)
for r in result:
print(r.text)
except Exception as e:
print(e)
if __name__ == "__main__":
main()
|