python硒选择一个新元素
我正在尝试使用Selenium列出推文列表,并且在录制第一个元素后,我在选择下一个元素时遇到了麻烦。这是我的代码:
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
有多个元素,xpath
下一个。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该包括标记,但是如果我正确理解您,您想用
// div [@data-testid =“ TweetText”]
的xpath检索所有元素。如果是这种情况,您只需要使用
find_elements
而不是find_element
,它将返回所有元素的列表,然后您可以简单地将其用于简单的循环。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 offind_element
and it will return a list of all elements that you can then use in a simple for loop.