使用python selenium为poc在twitter上发布推文
我正在尝试使用python和selenium在Twitter上发布一条推文 但是在发布的时候,它给了我超时错误 我已经把代码准备好了使用python selenium为poc在twitter上发布推文,python,selenium,Python,Selenium,我正在尝试使用python和selenium在Twitter上发布一条推文 但是在发布的时候,它给了我超时错误 我已经把代码准备好了 from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import e
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
import os
# create a new Chrome session
chromedriver = "C:/Users/LENOVO/Desktop/chromedriver/chromedriver.exe"
chromedriver = "C:/Users/LENOVO/Desktop/chromedriver/chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
#driver = webdriver.Chrome()
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("https://twitter.com/login")
# get the username textbox
login_field = driver.find_element_by_class_name("js-username-field")
login_field.clear()
# enter username
login_field.send_keys("")
time.sleep(1)
#get the password textbox
password_field = driver.find_element_by_class_name("js-password-
field")
password_field.clear()
#enter password
time.sleep(10)
password_field.send_keys("")
time.sleep(10)
password_field.submit()
autotw1 = WebDriverWait(driver, 140).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "div[id='tweet-box-home-
timeline']")))
autotw1.send_keys("""Just a testing """)
tweet = driver.find_element_by_xpath("//span[@class='add-tweet-button
']//following-sibling::button[contains(@class,'tweet-action')]")
tweet.click()
我得到下面的错误
TimeoutException回溯(最后一次最近调用) 在里面 39 40自动W1=WebDriverWait(驱动程序,140)。直到(
--->41 EC.元素可点击((By.CSS选择器,“div[id='tweet-box-home-timeline']))您在脚本中提供了错误的xpath和CSS值。请使用您自己的凭证在scrip下面运行,不要忘记更新chrome驱动程序路径
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.action_chains import ActionChains
# Open Chrome
driver = webdriver.Chrome('Path to chrome driver')
driver.get("https://twitter.com/login")
# get the username textbox
login_field = driver.find_element_by_class_name("js-username-field")
login_field.clear()
# enter username
login_field.send_keys("")
time.sleep(1)
# get the password textbox
password_field = driver.find_element_by_class_name("js-password-field")
password_field.clear()
# enter password
password_field.send_keys("")
password_field.submit()
autotw1 = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='css-1dbjc4n r-xoduu5 r-1sp51qo r-mk0yit r-13qz1uu']")))
ActionChains(driver).move_to_element(autotw1).click(autotw1).send_keys("Just a testing").perform()
tweet = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='css-901oao css-16my406 css-bfa6kz r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0']//span[@class='css-901oao css-16my406 r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0'][contains(text(),'Tweet')]")))
tweet.click()
您可以使用此定位器:
By.CLASS\u NAME,“DraftEditor root”
您必须单击该元素以调出其他元素来编写tweet,即:By.CLASS_NAME,'public DraftEditorPlaceholder root')
,并使用ActionChains
发送文本
首先,以下几点:
from selenium.webdriver import ActionChains
提交登录后,请尝试以下操作:
password_field.submit()
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("""Just a testing """).perform()
sendTw = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[@role="button"]/div/span/span')))
sendTw.click()
在执行脚本时,您观察到了什么?您成功通过身份验证了吗?我可以成功登录。但在发推特的时候,我收到错误…超时…附上试图单击的文件的屏幕截图。@vinod mehta:请检查上面的解决方案@Rock它正在工作。但是最后的推文没有发布…我无法理解代码的内部工作。请对此提供帮助…我已经验证了这个解决方案,并且它正在工作在本地。可能存在不适用的问题,因此添加了等待。请检查一下,您是否愿意接受这个答案并点击投票按钮如果您的问题是,您是否愿意接受我的答案resolved@frianH.It作品请发送截图…我想了解如何找到ist元素和下一步。。