Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
在使用Selenium和Python时,如何找到元素的特定父/祖先?_Python_Selenium_Web Scraping - Fatal编程技术网

在使用Selenium和Python时,如何找到元素的特定父/祖先?

在使用Selenium和Python时,如何找到元素的特定父/祖先?,python,selenium,web-scraping,Python,Selenium,Web Scraping,我目前正在从事一个项目,该项目从动态生成的页面中提取内容并进行记录 下面是我正在使用的结构的一个示例 我可以使用以下工具向下滚动页面并找到每个产品卡的具体详细信息: cards=driver。通过css选择器(“div[class^='product-card\uu Content']”查找元素 从每张卡片中,我可以提取标题、价格和其他变量,并根据需要存储它们 问题是,我想在它们后面附加“category”,它恰好作为标题出现在每一组项目的上方,例如上面的flower后面是prerolls。当

我目前正在从事一个项目,该项目从动态生成的页面中提取内容并进行记录

下面是我正在使用的结构的一个示例

我可以使用以下工具向下滚动页面并找到每个产品卡的具体详细信息:
cards=driver。通过css选择器(“div[class^='product-card\uu Content']”查找元素

从每张卡片中,我可以提取标题、价格和其他变量,并根据需要存储它们

问题是,我想在它们后面附加“category”,它恰好作为标题出现在每一组项目的上方,例如上面的flower后面是prerolls。当页面向下滚动时,将显示其中一个新项目,后面是相关项目

我已经能够访问这个的第一个实例,但是无论我把下面的代码放在我的循环中的什么地方,它都不会提取更新的值,只有初始值

category=driver。通过css选择器(“div[class^='products-grid\uuuu ProductGroupTitle']”查找元素。文本

我试图找到一种方法,将其作为当前卡的父/祖,但这个概念对我来说仍然是新的,超出了我对如何解决它的理解。找到“最近的”产品组标题是正确的方法吗?如果是这样,我将如何在页面滚动时动态地这样做


谢谢

您可以使用这个
xpath
-

//div[text()="Flower"]/following-sibling::div/div

只需将文本“Flower”与其他类别一起更改,您就会找到特定类别下的所有卡片。

要获取所有产品组名称,您可以使用CSS选择器

div[class^='products-grid__ProductGroupTitle']
注意:
^=
在CSS选择器中,表示以开头

从那里,我们可以将产品组插入到XPath中,并找到该组下每个产品的所有详细信息

# loop through all the product names
for product_group_name in driver.find_elements_by_css_selector("div[class^='products-grid__ProductGroupTitle']")
    # loop through each product card
    for product in driver.find_elements_by_xpath("//div[starts-with(@class,'products-grid__ProductGroup')][./div[starts-with(@class,'products-grid__ProductGroupTitle')][text()='" + product_group_name.text + "']]//div[starts-with(@class,'consumer-product-card__InViewContainer')]")
        # get individual product info
        brand = product.find_element_by_css_selector("div[class^='product-information__Brand']")

        # if you use an XPath, make sure you include a dot (.) at the start of the locator
        # brand example using XPath
        brand = product.find_element_by_xpath(".//div[starts-with(@class,'product-information__Brand')]")

        title = product.find_element_by_css_selector("div[class^='product-information__TitleContainer']:not(.mobile-and-card)")
        # ... and so on
问题是。。。该页面已被破坏,因此在修复之前,代码可能无法工作。如果您查看dev控制台,您将看到重复的错误消息

[mobx.array]尝试读取超出范围(0)的数组索引(0)。请先检查长度。MobX不会跟踪越界索引


每次触发该消息时,页面似乎都会失去对页面上元素的跟踪。

您能分享您的代码吗?如果当前有两次不同的尝试运行,则上的临时链接将被禁用。根据我现在收到的这张票的反馈,它仍然缺少价格和品牌。一旦我能够将这些添加到数据点中,我就几乎完成了。谢谢非常感谢。当我尝试使用它时,我得到以下错误:
selenium.common.exceptions.InvalidSelectorException:Message:Given css选择器表达式”//div[text()='Flower']/following sibling::div/div“无效:SyntaxError:Document.queryselectoral:'//div[text()='Flower']/以下同级::div/div'不是有效的选择器
您能否澄清,我如何从中访问每张卡的特定元素?比如品牌和价格?谢谢。谢谢,我不确定这是否是由于您在页面中提到的问题造成的,但我收到了以下错误消息:
Traceback(最近一次调用):文件“C:\python\scrap4.py”,第28行,在驱动程序中的产品组名称。通过css\u选择器(“div[class^='products-grid\uu ProductGroupTitle'])查找元素.text:AttributeError:“list”对象没有属性“text”
`@T0ne不,这是我的错误。我现在已经修复了代码。
raise exception\u class(message,screen,stacktrace)selenium.common.exceptions.NoSuchElementException:message:无法定位元素:div[class^='product-information\uu Brand']
我已经尝试使用我在上一次迭代中知道的方法来解决各种错误,但由于某些原因,它不会用此代码识别品牌。快速更新。我把范围缩小到下面的代码,这给了我类别和项目名称。但我不知道如何增加价格或品牌。如果能澄清我做错了什么,那就太好了。