简体   繁体   English

用硒找不到元素

[英]Can't find Element with Selenium

Im trying to scrap the price of a flight from the Google Flights website using Selenium but said element does not show up anywhere, not even when scraping the whole page. 我试图使用Selenium从Google Flights网站上取消一次航班的价格,但表示该元素不会显示在任何地方,即使是在整个页面上也没有显示。 Ive read that it might be due to it being in a different frame, but how would I know in which frame it is. 我已经读到它可能是由于它处于不同的框架中,但是我怎么知道它在哪个框架中。

Here is the website: https://www.google.es/flights?lite=0#flt=/m/0h3tv./m/05qtj.2018-12-14;c:EUR;e:1;a:FR;sd:1;t:f;tt:o 这是网站: https : //www.google.es/flights?lite=0#flt=/m/0h3tv./m/05qtj.2018-12-14; c: EUR; e:1;a: FR ; sd:1; t:f; tt:o

The price I'm looking for is: 32 € 我想要的价格是:32€

And here is my code: 这是我的代码:

from bs4 import BeautifulSoup as soup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")

d = webdriver.Chrome('/Users/davidgarciaballester/Desktop/chromedriver', options=chrome_options)

url='https://www.google.es/flights?lite=0#flt=/m/0h3tv./m/05qtj.2018-12-14;c:EUR;e:1;a:FR;sd:1;t:f;tt:o'
d.get(url)



precios = soup(d.page_source, 'html.parser').findAll('jsl',{'jstcache':'9322'})


print(precios)

d.quit();

Am I missing something? 我想念什么吗? Thanks in advance. 提前致谢。

EDIT 1: jstcache changed value to 9322 编辑1:jstcache的值更改为9322

You can use the following CSS selector combination: 您可以使用以下CSS选择器组合:

from selenium import webdriver

d = webdriver.Chrome()
d.get("https://www.google.es/flights?lite=0#flt=/m/0h3tv./m/05qtj.2018-12-14;c:EUR;e:1;a:FR;sd:1;t:f;tt:o")
item = d.execute_script("return document.querySelector('.flt-subhead1.gws-flights-results__price.gws-flights-results__cheapest-price span + jsl')")
print(item.text)
d.quit()
from bs4 import BeautifulSoup as soup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options



d = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')

url='https://www.google.es/flights?lite=0#flt=/m/0h3tv./m/05qtj.2018-12-14;c:EUR;e:1;a:FR;sd:1;t:f;tt:o'
d.get(url)

page = soup(d.page_source, 'html.parser')

precios = page.findAll('jsl',{'jstcache':'9322'})

print(precios)

d.quit();

worked for me: 为我工作:

print (precios[0].text)

gave me €32 给了我€32

Ok figured out what was going on. Ok知道发生了什么事。 I wasn't giving the driver enough time to load the page. 我没有给驱动程序足够的时间来加载页面。 Fixed this by stalling for a few seconds after loading the page. 通过在加载页面后停顿几秒钟来解决此问题。

Working code: 工作代码:

from bs4 import BeautifulSoup as soup
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options



d = webdriver.Chrome('C:/Users/David/Desktop/chromedriver.exe')

url='https://www.google.es/flights?lite=0#flt=/m/0h3tv./m/05qtj.2018-12-14;c:EUR;e:1;a:FR;sd:1;t:f;tt:o'
d.get(url)

time.sleep(5)

page = soup(d.page_source, 'html.parser')

precios = page.findAll('jsl',{'jstcache':'9322'})

print(precios)

d.quit()

EDIT 1: As Idlehands pointed out the jstcache number is probably dynamic and changes over time, so this aproach was not well thought. 编辑1:正如Idlehands指出的那样,jstcache数量可能是动态的,并且会随着时间而变化,因此,这种想法并未得到很好的考虑。 Instead I'm now using the following CSS selector combination QHarr suggested. 相反,我现在使用QHarr建议的以下CSS选择器组合。 Working code: 工作代码:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

whitelist = set('abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789')

chrome_options = Options()
chrome_options.add_argument("--headless")

d = webdriver.Chrome('C:/Users/David/Desktop/chromedriver.exe', options=chrome_options)

url='https://www.google.es/flights?lite=0#flt=/m/0h3tv./m/05qtj.2018-12-14;c:EUR;e:1;a:FR;sd:1;t:f;tt:o'
d.get(url)

time.sleep(2)

precio = d.execute_script("return document.querySelector('.flt-subhead1.gws-flights-results__price.gws-flights-results__cheapest-price span + jsl')").text
precio = ''.join(filter(whitelist.__contains__, precio))

print(precio)

d.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM