【问题标题】:can`t send tweet with selenium and python无法使用 selenium 和 python 发送推文
【发布时间】:2020-01-16 14:42:09
【问题描述】:

您好,我需要以下代码的帮助.... 我正在使用 selenium 和 python 来自动发送推文:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

driver = webdriver.Chrome(executable_path = r'C:/webDriver/chromedriver.exe')
driver.get("https://twitter.com/login")

username = driver.find_element_by_css_selector("input[placeholder='Phone, email or username']")
password= driver.find_element_by_css_selector("input[class='js-password-field']")
username.send_keys("xxxx")
password.send_keys("xxxx")

submit = driver.find_element_by_xpath("//button[text()='Log in']")
submit.click()
time.sleep(2.4)
autotw1 = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '//*[@id="tweet-box-home-timeline"]/div'))).click
autotw1.send_keys("""testing """)  

time.sleep(5)
tweet = driver.find_element_by_xpath('//span[@class="add-tweet-button "]//following-sibling::button[contains(@class,"tweet-action")]')
tweet.click()

但它给了我错误:

raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

我认为错误来自 twitter 网站新样式的 xpath 原因!

【问题讨论】:

  • 您应该知道,从您自己的问题中删除内容(一旦回答了所述问题),是真的不赞成并且可以/将被制裁为故意破坏。此外 - 它仍然在您的问题的edit history 中可见。如果有一个真实的、充分的理由说明信息应该消失(例如意外发布个人信息等),您应该标记该问题,以便版主可以为您清理它(包括历史记录)。
  • @coode3r 我不明白您为什么要同时编辑您的问题和我的答案,并尝试没有意义的更改。
  • @frianH 如果他们只编辑了你的答案或他们的问题,我会假设他们犯了一个诚实的事故/错误(选择东西,然后在空格键上滑动或其他东西)。由于他们在两者上都这样做了,我只能想象两个原因:无意识的破坏行为(尽管我觉得这不太可能)或者他们想将代码作为“他们自己的工作”(可能是为了学校或工作)传递并试图隐藏这样一个事实他们实际上得到了帮助......

标签: python selenium google-chrome selenium-webdriver twitter


【解决方案1】:

您可以使用此定位器:By.CLASS_NAME, 'DraftEditor-root'

您必须点击该元素才能调出其他元素来编写推文,即:By.CLASS_NAME, 'public-DraftEditorPlaceholder-root'),并使用ActionChains发送文本。

导入后:

from selenium.webdriver import ActionChains

试试下面的代码:

#submit login
submit = driver.find_element_by_xpath("//button[text()='Log in']")
submit.click()

autotw1 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'DraftEditor-root')))
autotw1.click()

element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.CLASS_NAME, 'public-DraftEditorPlaceholder-root')))
ActionChains(driver).move_to_element(element).send_keys('testing').perform()

sendTw = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Tweet')))
sendTw.click()

【讨论】:

    猜你喜欢
    • 2020-09-02
    • 2020-07-30
    • 2015-03-22
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    相关资源
    最近更新 更多