Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xpath没有使用Splinter/Selenium Python 3选择正确的元素_Python_Python 3.x_Selenium_Xpath_Splinter - Fatal编程技术网

Xpath没有使用Splinter/Selenium Python 3选择正确的元素

Xpath没有使用Splinter/Selenium Python 3选择正确的元素,python,python-3.x,selenium,xpath,splinter,Python,Python 3.x,Selenium,Xpath,Splinter,我不确定我是否犯了一个愚蠢的错误,我已经搜索了所有地方,但我无法找出这一个。我非常感谢你的帮助 我正试图制作一个刮板来刮谷歌地图包数据。我用斯普林特来做。我已经成功地选择了每个地图包项目的div,但是我想遍历并选择每个div的标题(和其他元素) 但是,当我尝试这样做时,它总是选择第一个元素的标题,即使我正在对单个元素运行find_by_xpath 这是我的密码: from splinter import Browser from selenium import webdriver import

我不确定我是否犯了一个愚蠢的错误,我已经搜索了所有地方,但我无法找出这一个。我非常感谢你的帮助

我正试图制作一个刮板来刮谷歌地图包数据。我用斯普林特来做。我已经成功地选择了每个地图包项目的div,但是我想遍历并选择每个div的标题(和其他元素)

但是,当我尝试这样做时,它总是选择第一个元素的标题,即使我正在对单个元素运行find_by_xpath

这是我的密码:

from splinter import Browser
from selenium import webdriver
import time

chrome_options = webdriver.ChromeOptions()
browser = Browser('chrome', options=chrome_options)


browser.visit("https://google.com")

browser.fill('q', 'roofing laredo tx')
# Find and click the 'search' button
time.sleep(5)
button = browser.find_by_name('btnK')
# Interact with elements
button.click()
time.sleep(5)
maps_elements = browser.find_by_xpath("//div[contains(@class,'VkpGBb')]")

for map_element in maps_elements:
    # print(map_element.text)
    title = map_element.find_by_xpath("//div[contains(@class,'dbg0pd')]/span").text
    print(title)
所以我想要的是: 弗洛雷斯屋顶与建筑 HBC屋面 麦卡伦谷屋面公司

但是我得到了

弗洛雷斯屋顶与建筑 弗洛雷斯屋顶与建筑 J J Flores屋面与施工

编辑:

您得到了重复的结果,因为从循环中选择根元素
/
它应该是相对的,或者
/
来选择子元素,但它仍然不起作用,可能会分裂bug。但是尝试使用CSS选择器

for map_element in maps_elements: 
    # select relative but failed
    #title = map_element.find_by_xpath("./div[contains(@class,'dbg0pd')]/span")
    title = map_element.find_by_css("div[class*='dbg0pd'] > span").text
    print(title)

键入变量,从中删除
s

title = maps_elements.....
#title = map_element.....
编辑:

您得到了重复的结果,因为从循环中选择根元素
/
它应该是相对的,或者
/
来选择子元素,但它仍然不起作用,可能会分裂bug。但是尝试使用CSS选择器

for map_element in maps_elements: 
    # select relative but failed
    #title = map_element.find_by_xpath("./div[contains(@class,'dbg0pd')]/span")
    title = map_element.find_by_css("div[class*='dbg0pd'] > span").text
    print(title)

键入变量,从中删除
s

title = maps_elements.....
#title = map_element.....
更改您的代码:

maps_elements = browser.find_by_xpath("//div[contains(@class,'VkpGBb')]")

for map_element in maps_elements:
    # print(map_element.text)
    title = maps_elements.find_by_xpath("//div[contains(@class,'dbg0pd')]/span").text
    print(title)

更改您的代码:

maps_elements = browser.find_by_xpath("//div[contains(@class,'VkpGBb')]")

for map_element in maps_elements:
    # print(map_element.text)
    title = maps_elements.find_by_xpath("//div[contains(@class,'dbg0pd')]/span").text
    print(title)


这是正确的,因为您不能在for循环中声明变量,然后在其中创建该变量。您需要在初始化循环之前创建变量,以使其工作

title_elements = browser.find_by_xpath("//div[contains(@class,'dbg0pd')]/span")

for title_element in title_elements:
    title = title_element.text
    print(title)

这是正确的,因为您不能在for循环中声明变量,然后在其中创建该变量。您需要在初始化循环之前创建变量,以使其工作

title_elements = browser.find_by_xpath("//div[contains(@class,'dbg0pd')]/span")

for title_element in title_elements:
    title = title_element.text
    print(title)

你说得对!我真傻。我已经解决了这个问题,但仍然会遇到相同的错误:(再次感谢您的回复。您确定是这样吗?我觉得一切都一样。您指的是SERP的哪一部分?谢谢!!这很有效。一定是一个分裂错误。CSS选择器工作得很好。谢谢您的时间!您是对的!我真傻。我已经修复了,但仍然得到相同的错误:(再次感谢您的回复。您确定是这样吗?我觉得一切都一样。您指的是SERP的哪一部分?谢谢!!这很有效。一定是一个分裂错误。CSS选择器很有效。谢谢您的时间!嗨,Yu,谢谢您的评论,这将有助于获得标题,但我也想获得评论明星,评论编号、网站和电话号码。我想通过选择包含这些内容的div,然后在其中搜索,可以获得其中的每一个内容,但似乎不可能在一个元素中搜索?嗨,Yu,谢谢你的评论,这将有助于获得标题,但我也想获得评论明星,r查看号码、网站和电话号码。我想通过选择包含这些内容的div,然后在其中进行搜索,可以获得其中的每一个,但似乎不可能在一个元素中进行搜索?