gpt4 book ai didi

python - 使用 Selenium Python chrome webdriver 发推文

转载 作者:行者123 更新时间:2023-12-01 01:54:15 24 4
gpt4 key购买 nike

我不知道为什么这段代码不能正常工作。该程序用于自动查找用户名、密码的相应字段,并输入要发送的推文的一些数据。我正确选择了页面的元素,但无法在其中输入数据并获得

NoSuchElement

异常。

from selenium import webdriver
import time
import os
import selenium

chromedriver = "mypathto/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)

driver.get('https://twitter.com/login')
username = driver.find_element_by_name("session[username_or_email]")
password= driver.find_element_by_name("session[password]")
username.send_keys("myusername")
password.send_keys("password")



submit = driver.find_element_by_class_name("submit EdgeButton EdgeButton--primary EdgeButtom--medium")
submit.click()

autotw1= driver.find_element_by_id('tweet-box-home-timeline')
autotw1.send_keys("""Just a testing """)
print "Tweet probably sent"

driver.save_screenshot("mybotfunc.png")

最佳答案

您可以使用此代码:

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'D:/Automation/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("your username")
password.send_keys("your password")

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

autotw1 = WebDriverWait(driver, 10).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()

为什么你的代码不起作用:

此名称有 3 个可用元素:session[username_or_email]
有几个这样的:session[password]
和 2 个具有此 id 的:tweet-box-home-timeline

关于python - 使用 Selenium Python chrome webdriver 发推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50395773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com