在搜索栏中使用 send_keys (Python) 后如何解析来自网站的搜索结果?

How to parse search results from website after having used send_keys (Python) in search bar?

我的问题与此有关post:

我能够执行对前一个问题的回答,但无法从 Chrome 通过循环 book 导航到的网站抓取数据。我只找到了可以显示如何在我的代码中从 d 抓取数据的答案,但在使用 send_keys.

后却没有从搜索结果中抓取数据

我尝试访问该元素但无法访问,我想在搜索 book 后从生成的网站中抓取数据,然后进入下一轮循环。

我试过了:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
d = webdriver.Chrome('mypath/chromedriver.exe')
books = ['9780062457738']
for book in books:
  d.get('https://www.bol.com/nl/')
  e = d.find_element_by_id('searchfor')
  f = print(e.send_keys(book, Keys.ENTER))

我也尝试过不使用 print() 函数,但是如果我输入 f? 它 returns 没有真正的元素 我得到:

Type:        NoneType
String form: None
Docstring:   <no docstring>

非常欢迎在 提交搜索查询后如何解析书籍作者、书名或其他信息的数据!

要提取和书名,即 不在乎的微妙艺术,您需要诱导 for the visibility_of_element_located() and you can use either of the following :

  • 使用CSS_SELECTOR:

    driver.get("https://www.bol.com/nl/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.js-confirm-button>span"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#searchfor"))).send_keys("9780062457738")
    driver.find_element_by_css_selector("button[type='submit']").click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.product-title"))).get_attribute("innerHTML"))
    
  • 使用XPATH:

    driver.get("https://www.bol.com/nl/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Accepteren']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='searchfor']"))).send_keys("9780062457738")
    driver.find_element_by_xpath("//button[@type='submit']").click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(@class, 'product-title')]"))).get_attribute("innerHTML"))
    
  • 控制台输出:

    The Subtle Art of Not Giving a F*ck
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC