Crawling JavaScript site with selenium (python) returns error: Message: no such element: Unable to locate element:

Crawling JavaScript site with selenium (python) returns error: Message: no such element: Unable to locate element:

我不熟悉 python 和网络爬虫。我从 BeautifulSoup 开始,但很快了解到使用 JavaScript 的网站无法用 bs4 抓取,所以我开始使用 selenium。然而,Selenium 也 returns 一个错误,无法找到我试图抓取的元素(搜索框)。到目前为止,我还了解到,我试图抓取的页面可能使用 Angular,它以某种方式隐藏了我要查找的元素。有没有办法我仍然可以使用 selenium 或其他包来输入搜索查询和抓取 the site?

我试图找到的任何元素都找不到,我也尝试通过 xpathname 找到它们,但运气不佳。我相信 <app-root></app-root> 里面的任何东西都不能简单地用 selenium 找到。

到目前为止,这是我的代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
import time
import sys

chrome_driver_path = "path"   
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')

webdriver = webdriver.Chrome(
    executable_path=chrome_driver_path,
    options=chrome_options
)

useBaseURL = "https://ec.europa.eu/info/funding-tenders/opportunities/portal/screen/home"

with webdriver as driver:
    # timeout
    wait = WebDriverWait(driver, 10)

    driver.get(useBaseURL)
    
    searchbox = driver.find_element_by_class_name("ng-tns-c6-0 ui-inputtext ui-widget ui-state-default ui-corner-all ui-autocomplete-input ng-star-inserted")
  
    driver.close()

以下将键发送到该元素。您的错误是使用复合 class 名称作为 class 名称。我也加了下点击

driver.get(useBaseURL)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, " p-autocomplete > span > input"))).send_keys("AAA")

driver.find_element_by_css_selector('button.btn.btn-accent.btn-search').click()

导入

from selenium.webdriver.support import expected_conditions as EC