首页 > 解决方案 > 为什么使用 Selenium-WebElement (Python 3) xpath 的结果与 css 不同

问题描述

更新1:

简短的长问题

我想要我的谷歌保留页面上每条笔记的标题。

当我最终用 xpath 搜索它们时,它没有给我任何结果,但它适用于 css

我想知道为什么。

我所做的

  1. 为了保持代码简短,我将 google keep 页面保存到 hdd 并将其加载到驱动程序中

  2. 在 html 代码中搜索笔记,并在 Parent_Liste 中获取它们的列表

  3. 在 Parent_Liste 中显示每个的 outerHTML 以确保标题在 HTML 代码中
  4. 使用 xpath 搜索 Parent_Liste 搜索中的每个标题
  5. 使用 css 搜索 Parent_Liste 搜索中的每个标题

下面是第一个元素的 python 控制台的代码和输出。第一个元素有标题:stackoverflowCaption

from selenium import webdriver

driver = webdriver.Chrome()

#step 1
driver.get("file://d:/google notizen.html") #on hdd saved google keep page

#step 2
Parent_Liste = driver.find_elements_by_xpath("//div[contains(@class,'IZ65Hb-n0tgWb IZ')]//div[@aria-label='Titel']/parent::*")

print("Number of Elements in Parent_Liste: " + str(len(Parent_Liste)))
print("=========================\n")

counter = 0

for each in Parent_Liste:
    print("\nElement of Parent_Liste: " + str(counter) + "\n======================") #prettify the output

#step 3
    print('outerHTML each Parent:\n' + each.get_attribute('outerHTML')) #here I get the outerHTML of the each-parent only for demonstraition


#step 4
    TitelElementxpath = each.find_element_by_xpath("//div[@aria-label='Titel']")  #here I want the WebElement with the caption
    print("xpath Result: " + TitelElementxpath.get_attribute('outerHTML')) #outerHTML has not the caption in the code
    print("xpath Result.text: " + TitelElementxpath.text)     # .text is empty

#step 5
    TitelElementcss = each.find_element_by_css_selector("div[aria-label='Titel']") #here I want the WebElement with the caption
    print("css Result: " + TitelElementcss.get_attribute('outerHTML')) #outerHTML has the caption in the code
    print("css Result.text: " + TitelElementcss.text)         # .text has the caption

    counter = counter + 1

Pyhton-Console 中第一个元素的输出


Number of Elements in Parent_Liste: 7
=========================


Element of Parent_Liste: 0
======================
outerHTML each Parent:

<div class="IZ65Hb-s2gQvd"><div role="button" class="Q0hgme-LgbsSe Q0hgme-Bz112c-LgbsSe IZ65Hb-nQ1Faf VIpgJd-LgbsSe" aria-label="Notiz anpinnen" aria-disabled="false" aria-pressed="false" style="user-select: none;" tabindex="0"></div><div class="IZ65Hb-nQ1Faf-cQwEuf"></div><div class="IZ65Hb-hYUzqc"><div class="IZ65Hb-HiaYvf-neVct"></div></div><div contenteditable="false" aria-multiline="true" role="textbox" aria-label="Titel" class="notranslate IZ65Hb-YPqjbf r4nke-YPqjbf" dir="ltr" style="background-color: rgb(255, 255, 255);">stackoverflowCaption</div><div contenteditable="false" aria-multiline="true" role="textbox" aria-label="Notiz" class="notranslate IZ65Hb-YPqjbf h1U9Be-YPqjbf" dir="ltr" style="">stackoverflowNote</div><div class="IZ65Hb-jfdpUb xFQqWe"><div class="Q0hgme-XPtOyb YPIHXb-Hjleke-XPtOyb" style="display: none;"><div role="button" class="Q0hgme-LgbsSe XPtOyb-bN97Pc VIpgJd-LgbsSe" tabindex="0" style="user-select: none;"><div class="XPtOyb-Bz112c"></div><label class="XPtOyb-fmcmS"></label></div><div role="button" class="Q0hgme-LgbsSe Q0hgme-Bz112c-LgbsSe XPtOyb-VkLyEc VIpgJd-LgbsSe" tabindex="0" aria-label="Quelle entfernen" style="user-select: none;"></div></div><div class="Q0hgme-XPtOyb zyxPWd-XPtOyb" style="display: none;"><div role="button" class="Q0hgme-LgbsSe XPtOyb-bN97Pc VIpgJd-LgbsSe" tabindex="0" style="user-select: none;"><div class="XPtOyb-Bz112c"></div><label class="XPtOyb-fmcmS"></label></div><div role="button" class="Q0hgme-LgbsSe Q0hgme-Bz112c-LgbsSe XPtOyb-VkLyEc VIpgJd-LgbsSe" tabindex="0" aria-label="Erinnerung löschen" style="user-select: none;"></div></div><div class="IZ65Hb-jfdpUb-fmcmS" aria-label="Erstellt um 09:47">Bearbeitet: 09:47</div></div><div class="IZ65Hb-kODWGd" style="display: none;"></div></div>
xpath Result: <div contenteditable="true" aria-multiline="true" role="textbox" aria-label="Titel" class="notranslate IZ65Hb-YPqjbf r4nke-YPqjbf" style="display: none; background-color: rgb(255, 255, 255);" tabindex="0"></div>
xpath Result.text: 
css Result: <div contenteditable="false" aria-multiline="true" role="textbox" aria-label="Titel" class="notranslate IZ65Hb-YPqjbf r4nke-YPqjbf" dir="ltr" style="background-color: rgb(255, 255, 255);">stackoverflowCaption</div>
css Result.text: stackoverflowCaption

标签: python-3.xselenium

解决方案


推荐阅读