【问题标题】:python selenium How do I choose the last message in the chatpython selenium 如何选择聊天中的最后一条消息
【发布时间】:2020-04-09 22:29:55
【问题描述】:

我使用库 python selenium 。 我想点击聊天中出现的最后一条消息。

这是网站:https://i.bc.game/i-x40k2jw-n/

# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

options = webdriver.ChromeOptions()
options.add_argument("javascript.enabled")
options.add_argument("--user-data-dir=chrome-data")
#options.add_argument("--incognito")
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")

driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)

driver.get("https://i.bc.game/i-x40k2jw-n/")
time.sleep(10)

while True:
    try:

        python_button = driver.find_element_by_css_selector(".coindrop-status")
        python_button.click()

    except:
        print("wait...")

【问题讨论】:

    标签: python python-2.7 selenium


    【解决方案1】:

    如果你只是粘贴到 HTML 中会容易得多。

    但是你可以找到所有元素,然后他们抓住最后一个,然后点击那个元素。

    例如:

    elements = driver.find_elements_by_class_name('coindrop-status')
    last_element = elements[-1]
    last_element.click()
    

    在本例中,elements 是一个列表,其中包含类名为 coindrop-status 的所有元素。 然后我们抓住最后一个元素,然后点击它。

    在您的示例中,您还捕获了所有异常,而您想要排除 NoSuchElementException,当您要查找的元素不存在时。

    【讨论】:

      猜你喜欢
      • 2022-11-13
      • 2018-10-09
      • 1970-01-01
      • 2019-03-23
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      相关资源
      最近更新 更多