Home > Blockchain >  python selenium select a new element
python selenium select a new element

Time:06-12

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.

CodePudding user response:

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:
   ...
  • Related