首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想从什么应用程序中抓取所有的元素,但是我对selenium python有问题

我想从什么应用程序中抓取所有的元素,但是我对selenium python有问题
EN

Stack Overflow用户
提问于 2021-10-01 13:06:08
回答 4查看 120关注 0票数 1

我想从什么应用程序中抓取所有的元素,但是我对selenium python有问题,这是我的代码:

代码语言:javascript
复制
from selenium import webdriver
from selenium.webdriver.common import keys
import time

driver = webdriver.Chrome("path/to/chromedriver")
driver.get("https://web.whatsapp.com/")
time.sleep(10)
input("Qr Code: ")
driver.implicitly_wait(10)

numbers = driver.find_elements_by_class_name('_ccCW')
for n in numbers:
   print(n.text)

我的脚本只抓取17个条目,whatsapp有3个类_ccCW FqYAR i0jNr,每个类中有一个有17个条目,所以如何刮掉这三个类

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-10-02 02:45:15

相同的WhatsApp元素可以使用2/3不同的xpath进行访问,因此对一个元素使用单个xpath不会返回文本alaways。

一些chrome扩展将有助于很好地识别xpath。我用了4个分机。

Navigation

  • TruePath

  • XPath
  1. HTML DOM
  2. xPath Finder

以下是whatsapp的固定元素

代码语言:javascript
复制
chat_search_box = "/html/body/div[1]/div[1]/div[1]/div[3]/div/div[1]/div/label/div/div[2]"
selected_profile_header_name = "//HEADER[@class='_23P3O']//SPAN[@class='_ccCW FqYAR i0jNr']"
chat_name = "/html/body/div[1]/div[1]/div[1]/div[4]/div[1]/header/div[2]/div[1]/div/span"
footer = "/html/body/div[1]/div[1]/div[1]/div[4]/div[1]/footer/div[1]"
footer_textbox = "/html/body/div[1]/div[1]/div[1]/div[4]/div[1]/footer/div[1]/div[2]/div/div[1]/div/div[2]"
msg_nav_arrow = "/html/body/div[1]/div[1]/div[1]/div[4]/div[1]/div[3]/div/div[1]/span/div/span[2]"

下面的xpath用于访问左侧窗格中的聊天名称。这些都是固定的,但div数是动态的。I= div # chat/group

代码语言:javascript
复制
pane_base = "(//div[@id='pane-side']//div[@class='_3OvU8'])"
pane_search_parent = lambda x : pane_base + "[" + str(x) + "]"
pane_group_last_sender = lambda i: pane_search_parent(i) + "//span[@class='FqYAR i0jNr']"
pane_user_sms =  lambda i : pane_search_parent(i) + "//span[@class='_ccCW FqYAR i0jNr']"
pane_sms_date = lambda i: pane_search_parent(i) + "//div[@class='_3vPI2']/div[@class='_1i_wG']"
pane_notif = lambda i: pane_search_parent(i) + "//span[@class='_23LrM']"
pane_username = lambda i: pane_search_parent(i) + "//div[@class='zoWT4']"

因为从chat访问消息是最困难的部分,相同的元素具有多个xpath。

代码语言:javascript
复制
msg_base = "(//div[@id='main']//div[@class='y8WcF']/div)[2]" #u have to change [2] with [n] to read specific msg

#u could find SENDER from any of 3 xpath depending on msg type.. sometime msg could be quoted text or first post or forwared msg or could be with image.
SENDER = [msg_base + "/div/div/div/div[1]/span[1]", msg_base + "//span[@class='a71At _3xSVM i0jNr']", msg_base + "//span[@class='_1BUvv']"]

#following item follows same as SENDER. please add 'msg_base' to each list items.
SENDER_NAME = ['/div/div/div/div[1]/span[2]']
SENDER_TEXT = ['/descendant::div/span/span[1]','//span[@class="i0jNr selectable-text copyable-text"]']
QUOTED_TEXT = ['/div/div/div/div[2]/div[1]/div/div/div/div/div[2]',"//span[@class='quoted-mention i0jNr']",'/div/div/div/div[1]/div[1]/div/div/div/div/div[2]']
QUOTED_SENDER = ['/div/div/div/div[2]/div[1]/div/div/div/div/div[1]/span[1]','/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/span',"//span[@class='a71At i0jNr']"]
TIME = ["//span[@class='kOrB_']",'/descendant::div[last()]']

请随时询问有关whatsapp使用python selenium抓取的任何问题。

票数 1
EN

Stack Overflow用户

发布于 2021-10-01 13:09:44

如果类名为_ccCWFqYARi0jNr,则尝试

代码语言:javascript
复制
classes_to_get = ["_ccCW", "FqYAR", "i0jNr"]

for kls in classes_to_get:
    numbers = driver.find_elements_by_class_name(kls)
    for n in numbers:
       print(n.text)
票数 1
EN

Stack Overflow用户

发布于 2021-10-01 13:22:29

尝试使用这个xpath:

代码语言:javascript
复制
//div[@role='gridcell']//span[@title]
代码语言:javascript
复制
numbers = driver.find_elements_by_xpath("//div[@role='gridcell']//span[@title]")
for n in numbers:
   print(n.text)
   #print(n.get_attribute("innerText")) # You can also use this line to print the text.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69406417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档