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

2024-05-13 23:02:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试自动发布到我的社交媒体帐户。我已经成功地编写了登录脚本,在登录后,我很难找到传递帖子的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>

Textarea HTML Screenshot

Notification Screenshot


Tags: fromimportdivbrowsercom元素byselenium
1条回答
网友
1楼 · 发布于 2024-05-13 23:02:01
  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:由于测试原因,我上传了两篇帖子,很抱歉,我无法以其他方式尝试

相关问题 更多 >