使用python selenium查找Twitter文本框元素

1bqhqjot  于 2023-02-06  发布在  Python
关注(0)|答案(1)|浏览(160)

我自己做了一个Twitter投诉机器人,如果网络中断,它会在我的ISP上发推文。代码工作得很完美,直到它必须找到Twitter文本框来输入推文。主要错误是:

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

我试过:

  • 添加时间延迟
  • 使用Firefox驱动程序而不是Google
  • 在tweet_at_provider()查找文本框之前添加页面刷新
  • 点击"Tweet"按钮,弹出文本框,然后尝试输入
  • 使用find. element_by_id,但twitter在每次页面加载时都会更改id

当我注解掉要测试的第一个函数调用时,它会查找并键入6/10次,但当两个函数都被调用时,tweet_at_provider()总是无法获取文本框,我得到StaleElement错误。

import selenium, time, pyautogui
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException, NoSuchElementException, StaleElementReferenceException

PROMISED_DOWN = 200
PROMISED_UP = 10
CHROME_DRIVER_PATH = "C:\Development\chromedriver.exe"
GECKODRIVER_PATH = "C:\\Users\\meeha\\Desktop\\geckodriver\\geckodriver.exe"
TWITTER_USERNAME = "my_username"
TWITTER_PASSWORD = "my_password"

class InternetSpeedTwitterBot():

    def __init__(self, driver_path):
        self.driver = webdriver.Chrome(executable_path=driver_path)
        self.down = 0
        self.up = 0

    def get_internet_speed(self):
        self.driver.get("https://www.speedtest.net/")
        self.driver.maximize_window()
        time.sleep(2)
        go = self.driver.find_element_by_xpath("//*[@id='container']/div/div[3]/div/div/div/div[2]/div[3]/div[1]/a/span[4]")
        go.click()
        time.sleep(40)

        self.down = self.driver.find_element_by_xpath("//*[@id='container']/div/div[3]/div/div/div/div[2]/div[3]/div[3]/div/div[3]/div/div/div[2]/div[1]/div[2]/div/div[2]/span")
        self.up = self.driver.find_element_by_xpath("//*[@id='container']/div/div[3]/div/div/div/div[2]/div[3]/div[3]/div/div[3]/div/div/div[2]/div[1]/div[3]/div/div[2]/span")

        print(f"Download Speed: {self.down.text} Mbps")
        print(f"Upload Speed: {self.up.text} Mbps")
        time.sleep(3)

    def tweet_at_provider(self):
        self.driver.get("https://twitter.com/login")
        self.driver.maximize_window()
        time.sleep(3)
        username = self.driver.find_element_by_name("session[username_or_email]")
        password = self.driver.find_element_by_name("session[password]")
        username.send_keys(TWITTER_USERNAME)
        password.send_keys(TWITTER_PASSWORD)
        password.submit()
        time.sleep(5)

        tweet_compose = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/header/div/div/div/div[1]/div[3]/a/div/span/div/div/span/span')
        tweet_compose.click()
        time.sleep(2)
        textbox = self.driver.find_element_by_xpath('//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div')
        textbox.send_keys(f"Hey @Ask_Spectrum, why is my internet speed {self.down.text} down / {self.up.text} up when I pay for {PROMISED_DOWN} down / {PROMISED_UP} up???")

bot = InternetSpeedTwitterBot(CHROME_DRIVER_PATH)

bot.get_internet_speed()
bot.tweet_at_provider()
eufgjt7s

eufgjt7s1#

我也有同样的错误,并发现HTML标记是立即改变,只要我输入的东西在twitter的文本框。
使用span标记的XPATH来解决这个问题,这个标记在我这边输入空格后出现。
标签是初始标签,当您没有提示任何文本时,只有在您键入任何内容之后
会变成,这时您必须复制XPATH并将其用于应用程序

相关问题