使用selenium(python)对JavaScript站点进行爬网时返回错误:消息:没有这样的元素:无法定位元素:
一般来说,我对python和webcrawling都是新手。我从BeautifulSoup开始,但很快就了解到使用JavaScript的站点不能通过使用selenium(python)对JavaScript站点进行爬网时返回错误:消息:没有这样的元素:无法定位元素:,python,selenium,web-scraping,web-crawler,Python,Selenium,Web Scraping,Web Crawler,一般来说,我对python和webcrawling都是新手。我从BeautifulSoup开始,但很快就了解到使用JavaScript的站点不能通过bs4进行爬网,因此我开始使用selenium。然而,Selenium也返回了一个错误,并且找不到我试图刮取的元素(搜索框)。到目前为止,我还了解到,我试图抓取的页面可能使用了角度,这不知何故隐藏了我正在寻找的元素。有没有一种方法我仍然可以使用selenium或其他软件包来输入搜索查询和爬网 我尝试查找的任何元素都找不到,我还尝试通过xpath或na
bs4
进行爬网,因此我开始使用selenium
。然而,Selenium也返回了一个错误,并且找不到我试图刮取的元素(搜索框)。到目前为止,我还了解到,我试图抓取的页面可能使用了角度
,这不知何故隐藏了我正在寻找的元素。有没有一种方法我仍然可以使用selenium或其他软件包来输入搜索查询和爬网
我尝试查找的任何元素都找不到,我还尝试通过xpath
或name
查找它们,但运气不佳。我相信只要使用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()
下面将向该元素发送键。您的错误是使用复合类名作为类名。我还添加了下一次单击
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
您的类名实际上是多个类。使用带有标记的css选择器,每个名称后跟一个。而不是空间。谢谢!工作很好。我没有使用下一次单击,而是使用了
键。RETURN
。有很大的区别吗?不管怎样,它工作得很好<代码>搜索=等待。直到(EC.元素可单击((通过.CSS选择器,“p-autocomplete>span>input”))搜索。发送键(“能量”+键。返回)