【问题标题】:Facebook post_box element. I cant find element by xpathFacebook post_box 元素。我无法通过 xpath 找到元素
【发布时间】:2017-07-19 17:20:29
【问题描述】:

Facebook post_box 元素。我无法通过 xpath 找到元素

嗨, 我学习使用 Python 在 Selenium Webdriver 中编写自动化测试脚本。我将制作“Facebook 海报脚本”。登录后我想点击 SendBox 并发送一些文本,但我无法用 Xpath 识别它。 Firebug 对此无能为力。谢谢帮忙。

类似这样的:

登录后

newpost=driver.find_element_by_xpath("i dont know correct xpath")
newpost.click()
newpost.send_keys("Hello!")
newpost.submit()

Firebug 的 Xpath 不工作

newpost = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div/div/div[2]/div[1]/div/div/div/div[2]/div/div[1]/div[1]/div[2]/div/div/div/div/div/div[2]/div/div/div/div")

【问题讨论】:

  • 坦率地说,我希望他们能做到,这样他们的网站(Facebook)就完全不能与 WebDriver 一起工作,以防止机器人/垃圾邮件。由于我假设您不为 Facebook 工作,因此如果您正在寻找可以学习的网站,请尝试以下操作:techbeamers.com/websites-to-practice-selenium-webdriver-online 或者如果您正在创建机器人/垃圾邮件......我真的不能祝您成功。跨度>

标签: python selenium xpath selenium-webdriver


【解决方案1】:

首先,永远不要使用绝对 xpath - 它的维护成本很高,而且看起来很糟糕。

据我了解,您需要单击带有占位符“您的想法”的输入字段。然后您可以使用值为 'status-attachment-mentions-input' 的 'data-testid' 属性。

如果你真的需要使用 xpath,试试这个: "//[@data-testid='status-attachment-mentions-input']"。您还可以使用 css 选择器并像这样找到 yo 元素:

inputField = driver.find_element_by_css_selector("[data-testid='status-attachment-mentions-input']")

【讨论】:

  • 不工作 :( 文件“D:/untitled/AllegroTest.py”,第 27 行 newpost = driver.find_element_by_css_selector("[data-testid="status-attachment-mentions-input"]") ^ SyntaxError: invalid syntax Process finished with exit code 1 Bad brackets: Correct version: driver.find_element_by_css_selector("[data-testid='status-attachment-mentions-input']") 与之前相同的错误:无法定位元素: [data-testid='status-attachment-mentions-input']
  • 我会编辑一个答案。在“status-at‌​tachment-mentions-in‌​put”周围使用单引号而不是双引号。
  • 和以前一样的错误:无法定位元素:[data-testid='status-attachment-mentions-input']
  • 你能贴一张你想要定位的元素的图片吗?这样我们就可以确定我们在谈论同样的事情?
  • 如果您将我的答案标记为有用,如果它对@PythonLearn 有帮助,那就太好了:)
【解决方案2】:

正确的解决方案:

newpost = driver.find_element_by_css_selector("[name='xhpc_message']")
newpost.click()
newpost.send_keys("Hello!")
newpost.submit()

@GVi 求帮助

【讨论】:

    猜你喜欢
    • 2022-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2021-01-13
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多