简体   繁体   中英

python selenium select a new element

I am trying to make a list of tweets using selenium and I am having trouble with selecting the next element after I record the first one. This is my code:

while count < tweets:
            sleep(1)
            ActionChains(self.twitterDriver).scroll_to_element(self.twitterDriver.find_element(by=By.XPATH, value='//div[@data-testid="tweetText"]')).perform()
            tweet = self.twitterDriver.find_element(by=By.XPATH, value='//div[@data-testid="tweetText"]')
            self.tweets.loc[len(self.tweets.index), "tweet"] = tweet.text
            count +=1

there are multiple elements with xpath of //div[@data-testid="tweetText"] and I can't figure out how to move to the next one.

You should include the markup but if I understand you correctly you want to retrieve all elements with an xpath of //div[@data-testid="tweetText"] . If this is the case, you just need to use the find_elements instead of find_element and it will return a list of all elements that you can then use in a simple for loop.

tweets = self.twitterDriver.find_elements(by=By.XPATH, value='//div[@data-testid="tweetText"]')
for tween in tweets:
   ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM