如何使用硒单击所有相同的文本范围?

How to click all of the same text span by using selenium?

我试试代码

allSpanDate = self.driver.find_element_by_xpath("//span[text()='08/28']").click()

但只有第一个元素被点击

所以我这样编辑但不起作用

allSpanDate = self.driver.find_element_by_xpath("//span[text()='08/28']")
for spanDate in allSpanDate:
    spanDate.click()

网站是

http://211.21.63.217:81/ticket_online.aspx

如何点击所有跨度文本为08/28?

from selenium import webdriver

class CenturyasiaxinbeiSpider(scrapy.Spider):
    name = 'CenturyasiaXinbei'
    allowed_domains = ['www.centuryasia.com.tw', '211.21.63.217:81']
    start_urls = ['http://211.21.63.217:81/ticket_online.aspx']

    def __init__(self):
        print('__init__')
        self.driver = webdriver.Chrome('/Users/motogod19/chromedriver')
        self.driver.get('http://211.21.63.217:81/ticket_online.aspx')

    def parse(self, response):
        allSpanDate = self.driver.find_element_by_xpath("//span[text()='08/28']")
        for spanDate in allSpanDate:
            spanDate.click()
        time.sleep(10)

看,当你这样做的时候:

allSpanDate = self.driver.find_element_by_xpath("//span[text()='08/28']")
for spanDate in allSpanDate:
    spanDate.click()

你一定是遇到了 web element is not iterabel 的错误。

那是因为你正在使用find_element,尝试使用find_elements

此外,我在本地尝试了以下代码,

allSpanDate = driver.find_elements_by_xpath("//span[text()='08/28']")
for spanDate in allSpanDate:
    ActionChains(driver).move_to_element(spanDate).perform()
    spanDate.click()
    time.sleep(1)

似乎可以完成您一直在寻找的工作。

进口:

from selenium.webdriver.common.action_chains import ActionChains
allSpanDate = driver.find_elements_by_xpath("//span[text()='08/28']")
for spanDate in allSpanDate:
    ActionChains(driver).move_to_element(spanDate).perform()
    spanDate.click()
    time.sleep(1)

driver.find_element_by_xpath returns 1 个元素(它将是页面上的第一个元素,带有 ("//span[text()='08/28']") 这个 xpath) 您需要使用 find_elements_by_xpath

方法获取所有元素