python中的硒错误:NoSuchElementException ./ancestor-or-self::form

问题描述 投票:2回答:3

我正在尝试制作python脚本,在python控制台中输入我的凭据和tweet文本,然后该脚本将使用selenium登录,然后进行tweet。所有任务都已完成。最后一次单击“ Tweet”按钮将引发错误。

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: ./ancestor-or-self::form

这里是与tweet按钮相关的检查元素代码:

<div class="css-1dbjc4n r-urgr8i r-42olwf r-sdzlij r-1phboty r-rs99b7 r-1w2pmg r-1n0xq6e 
r-1vuscfd r-1dhvaqw r-icoktb r-1fneopy r-o7ynqc r-6416eg r-lrvibr" 
aria-haspopup="false" disabled="" role="button" aria-disabled="true" 
data-testid="tweetButtonInline"> 
  <div class="css-901oao r-1awozwy r-jwli3a r-6koalj r-18u37iz r-16y2uox r-1qd0xha r-a023e6 
  r-vw2c0b r-1777fci r-eljoum r-dnmrzs r-bcqeeo r-q4m81j r-qvutc0" dir="auto">
    <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">Tweet
    </span>
    </span>
  </div>
</div>

现在,我要选择按钮并单击它。所以我写了如下的python代码:

ultim=driver.find_element_by_xpath('//span[@class="css-901oao css-16my406 r-1qd0xha r-ad9z0
r-bcqeeo r-qvutc0"]')
ultim.click()   

ultim=driver.find_element_by_xpath('//span[@data-testid="tweetButtonInLine"]')
ultim.click()

[请帮助我在python中使用硒单击那个Tweet按钮。我需要制作此脚本并提交学校项目。需要帮助,非常感谢。

javascript python html selenium twitter
3个回答
1
投票

以下代码可以发布测试tweet(请更改您的tweet内容,用户名和密码):

from selenium import webdriver

url = 'https://twitter.com/login'

username = 'your email'
password = 'your password'
tweet_data = 'This is a test.'
driver = webdriver.Chrome(executable_path=r'yourpath')
driver.maximize_window()
driver.get(url)

userid = driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[1]/input')
userid.send_keys(username)

psw = driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[2]/input')
psw.send_keys(password)

signinButton = driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/div[2]/button')
signinButton.click()

driver.implicitly_wait(10)

loggenInUrl = "https://twitter.com/home"

driver.get(loggenInUrl)
tweet = driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div/main/div/div/div/div[1]/div/div[2]/div[2]/div[1]/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div[1]/div[1]/div/div/div/div[2]/div/div/div/div')
tweet.send_keys(tweet_data)

post = driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div/main/div/div/div/div[1]/div/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/div/div/div[2]/div[3]/div/span/span')
post.click()

enter image description here

您可以简单地通过指向您想要xpath的元素并执行以下操作来复制xpath:enter image description here

请注意:不建议使用xpath。


0
投票

诱导WebDriverWaitelement_to_be_clickable()单击Tweet按钮。

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//span[text()="Tweet"]'))).click()

您需要导入以下库。

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

0
投票

尝试使用以下xpath:

ultim=driver.find_element_by_xpath('//span[normalize-space(text()="Tweet"]')
ultim.click() 
© www.soinside.com 2019 - 2024. All rights reserved.