简体   繁体   English

python selenium 选择一个新元素

[英]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.我正在尝试使用 selenium 制作推文列表,但在录制第一个元素后无法选择下一个元素。 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. //div[@data-testid="tweetText"]xpath有多个元素,我不知道如何移动到下一个元素。

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"] .您应该包含标记,但如果我理解正确,您希望检索所有带有//div[@data-testid="tweetText"] xpath 的元素。 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.如果是这种情况,您只需要使用find_elements而不是find_element ,它将返回所有元素的列表,然后您可以在简单的 for 循环中使用这些元素。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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