python - 用于定位文本区域元素的 Selenium 脚本不起作用

标签 python selenium selenium-webdriver automation selenium-chromedriver

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

最佳答案

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

我认为 3 题中有 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:因为测试我上传了两篇文章,很抱歉我不能尝试其他方式。

关于python - 用于定位文本区域元素的 Selenium 脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61581894/

相关文章:

python - 如何从列表/ndarray 中获取索引?

python - 这个 python 函数在寻找什么?

python - 始终允许使用 Selenium 在 Firefox 中进行地理定位

python - selenium.common.exceptions.TimeoutException : this error will getting when I'm trying to button click using python

python - 使用 Python 抓取 linkedin 连接,但只显示了一些 - Selenium 和 BeautifulSoup

Java - Selenium Webdriver - Ruby 绑定(bind) - 缺少特性和功能

python - 编写 django 登录页面(使用 Bootstrap)

c# - Selenium - 异常 - 连接关闭

vba - 如何使用 selenium 找到基于 wfd-id 的 html 元素?

python - 带有提示的 SQLAlchemy 查询 API 无法正常工作