Skip to content
Advertisement

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

I try the code

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

but only first element been clicked

so I edit like this but won’t work

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

website is

http://211.21.63.217:81/ticket_online.aspx

How to click all of the span text is 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)

Advertisement

Answer

See, when you did this :

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

you must have got web element is not iterabel error.

that is due to you are using find_element, try to use find_elements.

also, I tried the below code in my local,

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)

seems to do the job that you’ve been looking for.

Imports :

from selenium.webdriver.common.action_chains import ActionChains
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement