python selenium如何选择聊天中的最后一条消息

时间:2019-12-17 12:04:58

标签: python python-2.7 selenium

我使用库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...")

1 个答案:

答案 0 :(得分:0)

如果您刚粘贴到HTML中,将会容易得多。

但是您可以找到所有元素,然后它们抓住最后一个元素,然后单击该元素。

例如:

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

在此示例中, elements 是一个列表,其中包含所有类名称为 coindrop-status 的元素。 然后,我们获取最后一个元素,然后单击它。

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