用于定位textarea元素的Selenium脚本不起作用



我正在尝试自动将帖子发布到我的社交媒体帐户中。我已经成功地编写了登录脚本,登录后,我很难找到用于传递我的帖子的textarea元素,之后我将尝试在我的帖子上附加一个图像,制作帖子并注销。但现在,我在登录后被困在定位文本区域。这是代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
usernameStr = 'JonJames3872@gmail.com'
passwordStr = 'JamesJon'
textStr = 'Testing my Post'
browser = webdriver.Chrome()
browser.get(('https://accounts.kingsch.at/?client_id=com.kingschat&scopes=%5B%22kingschat%22%5D&redirect_uri=https%3A%2F%2Fweb.kingsch.at%2F'))
# fill in username and password

password = browser.find_element_by_name('password')
password.send_keys(passwordStr)

username = browser.find_element_by_class_name('field')
username.send_keys(usernameStr)
signInButton = browser.find_element_by_class_name('submit-btn')
signInButton.click()
# I HAVE LOGGED IN, NOW THIS IS WHERE MY CODE HAS A PROBLEM
text = browser.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/div/div[1]/div/div/div[1]/textarea")
text.send_keys(textStr)

这是元件,来自检查元件:

<textarea placeholder="What's happening?" class="KingingBox__input"></textarea>

文本区域HTML屏幕截图

  • 网址:https://accounts.kingsch.at/?client_id=com.kingschat&作用域=%5B%22Ckingschat%22%5D&redirect_uri=https://3A%2F%2Fweb.kingsch.在%2F
  • 示例用户名:JonJames3872@gmail.com(奇怪的是,这里用户名区分大小写(
  • 示例密码:JamesJon

通知屏幕截图

  1. 我插入代码在文本区域中写一些东西
  2. 我插入了上传照片的代码
  3. 我插入代码来发布你自己的帖子

我认为三个问题中有三个都完成了。

试试这个代码:

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

path = '/home/avionerman/Documents/stack'
browser = webdriver.Firefox(path)
browser.implicitly_wait(10)

usernameStr = 'JonJames3872@gmail.com'
passwordStr = 'JamesJon'
textStr = 'Testing my Post'
browser.get(('https://accounts.kingsch.at/?client_id=com.kingschat&scopes=%5B%22kingschat%22%5D&redirect_uri=https%3A%2F%2Fweb.kingsch.at%2F'))
# fill in username and password

password = browser.find_element_by_name('password')
password.send_keys(passwordStr)

username = browser.find_element_by_class_name('field')
username.send_keys(usernameStr)
signInButton = browser.find_element_by_class_name('submit-btn')
signInButton.click()

text = browser.find_element_by_class_name('KingingBox__input')
text.clear()
text.send_keys(textStr)
time.sleep(5)
image = browser.find_element_by_class_name('KingingBox__attachment-input').send_keys('/here/path/of_yours/th_574e7c36606306d94a4.jpg')
time.sleep(5)

inserted_photo = browser.find_element_by_class_name('KingingBox__attachments-list')
if inserted_photo.is_displayed():
print("Element found, photo uploaded successfully")
browser.find_element_by_css_selector('.KingingBox__submit-btn').click()
else:
print("Element not found")

在这一行:

browser.implicitly_wait(10)

我们确定浏览器将等待10秒的最长时间以使每个元素可见。如果文本区域出现的时间超过10秒,脚本将停止。如果你看到巨大的延迟,等待的时间就会增加。

此外,正如你所看到的,我使用了这条线:

text = browser.find_element_by_class_name('KingingBox__input')

以便定位文本区域。

在行中:

image = browser.find_element_by_class_name('KingingBox__attachment-input').send_keys('/here/path/of_yours/th_574e7c36606306d94a4.jpg')

我找到负责接受上传的输入标签,然后在上面发送我想要上传的文件的确切路径。

在最后一部分:

inserted_photo = browser.find_element_by_class_name('KingingBox__attachments-list')
if inserted_photo.is_displayed():
print("Element found, photo uploaded successfully")
browser.find_element_by_css_selector('.KingingBox__submit-btn').click()
else:
print("Element not found")

我将显示照片上传成功的元素保存到inserted_photo变量中。然后,如果显示此变量,这意味着照片上传正确。因此,既然我们有了文字和照片,我们就可以点击"发布"按钮了。

尽量使用非动态的静态属性,这样就不会有将来更改的危险。通过这种方式,您可以为代码创建稳定性。因为像您的示例中那样选择这样的xpath是有风险的。如果div或另一个标记将立即被排除或包含,那么xpath就没有用处了。

PS:因为测试,我上传了两篇帖子,很抱歉我不能用其他方式尝试

最新更新