【问题标题】:Element not found on phantomjs But working fine on chrome在 phantomjs 上找不到元素但在 chrome 上工作正常
【发布时间】:2016-11-23 13:16:40
【问题描述】:

我使用phantomjs 版本1.1.0phantomjs-2.1.1.exewindows

CONTINUE ACTIVATED

这是 HTML 代码

<div class="right-align" style="display: inline-block; vertical-align: middle; border-radius: 6px; margin-left: 20px; flex-grow: 100;">
<div>
<a class="t-next-pd continue-to-query button-text" style="background-color: rgb(11, 197, 216); color: rgb(255, 255, 255); height: 40px; padding: 0px 16px; text-decoration: none; display: inline-flex; font-weight: 500; font-size: 16px; border-radius: 4px; z-index: 100; cursor: pointer; align-items: center; justify-content: center; border: 1px solid rgb(11, 197, 216); width: 100%;">
<span style="display: inline-block;">CONTINUE</span>
</a>
</div>
</div>

我尝试使用

来定位元素
  1. 两个类的相对 xpath:.//*[@class='t-next-pd continue-to-query button-text'].//*[@class='right-align']
  2. 文字
  3. 部分文字
  4. 通过链接
  5. 通过 cssSelector
  6. 带文本的相对 xpath
  7. 通过绝对 xpath(最后优先级)
  8. 按类名
  9. javascript 执行器
  10. 隐式等待和显式等待

似乎没有任何效果

只有当我输入姓名和年龄时,CONTINUE 按钮才会被激活。 这是CONTINUE按钮未激活时的代码。

<div class="right-align" style="display: inline-block; vertical-align: middle; border-radius: 6px; margin-left: 20px; flex-grow: 100;">
<div>
<a class="button-text" disabled="" style="background-color: rgb(199, 199, 199); color: rgb(255, 255, 255); height: 40px; padding: 0px 16px; display: inline-flex; text-decoration: none; font-weight: 500; font-size: 16px; border-radius: 4px; z-index: 100; cursor: pointer; align-items: center; justify-content: center; width: 100%;">
<span style="display: inline-block;">CONTINUE</span>
</a>
</div>
</div>

CONTINUE DEACTIVATED

【问题讨论】:

  • 我在激活按钮上使用了这个a.t-next-pd.continue-to-query.button-text。这是一个 CSS 选择器,在我的情况下可以正常工作。

标签: selenium automation phantomjs headless-browser


【解决方案1】:

填写详细信息后稍等片刻。 PhantomJS 立即搜索激活的继续按钮。这很可能是时间问题。继续按钮可能需要 500 毫秒到 1 秒才能激活。

因此,当 PhantomJS 开始搜索激活的按钮时,它找不到它,因为 if 在 DOM 中还不存在。

如果您使用的是 Python,请使用 time.sleep(2)。它暂停 2 秒。这使 DOM 有时间将非激活代码更改为激活代码。

我用这个a.t-next-pd.continue-to-query.button-text 来找到激活的按钮。这是一个 CSS 选择器,它在我的测试用例中运行良好 :)

将其转换为您使用的语言。我正在使用 Python:-

# code
# to
# fill the form
time.sleep(2)
driver.find_element_by_css_selector('a.t-next-pd.continue-to-query.button-text')

【讨论】:

  • 还是不行。语言:Java。我已经使用过:- 1. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 2. TimeUnit.SECONDS.sleep(5); 3. 线程.sleep(1000);它们都不起作用。
  • @SaurabhNagpal 你能分享更多来自网页的代码和你的 Java 代码吗?我尝试了你给的任何东西,它对我有用。它将帮助人们找到问题所在。如果你分享代码后问题仍然存在,我会建立一个聊天室,我们可以在这里详细讨论:)
猜你喜欢
  • 1970-01-01
  • 2015-02-03
  • 2021-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 2018-01-24
相关资源
最近更新 更多