gpt4 book ai didi

javascript - QWebElement评估JavaScript this.click()方法不起作用

转载 作者:行者123 更新时间:2023-12-03 11:53:11 24 4
gpt4 key购买 nike

我已加载 QWebView 中的 HTML 表单。该 HTML 具有以下两个元素:

    <button class="submit1" tabindex="1" type="submit" id="submit1">accept</button>
<button class="submit2" tabindex="2" type="submit" id="submit2">decline</button>

在代码中我有以下内容:

    QWebView webView;
...
QWebElement button = webView->page()->mainFrame()->documentElement().findFirst("button[id=\"submit1\"]");
button.evaluateJavaScript("this.click();");

最后一行代码对我不起作用。此外,如果我有一个元素而不是按钮 - JavaScript click() 方法可以正常工作。

最佳答案

我无法逐一回答这个问题,因为从您提供给我们的信息中很难判断到底是什么问题,但我能做的是为您提供一些有关在 QWebView 中调试问题的指南。

1)我要检查的第一件事是,您是否确实拥有按钮元素。尝试这样做:

QWebView webView;
...
QWebElement button = webView->page()....findFirst("button[id=\"submit1\"]");
if button is not None:
button.evaluateJavaScript("this.click();");
else:
print "Oh dear!"

通过这个,我们想看看 findFirst() 是否真的获取了元素(我相信它确实如此,语法似乎没问题)。

2) 转到您正在处理的网页。那个按钮真的有用吗?某些脚本可能会向其注入(inject)“禁用”属性,从而使其不可点击。

3)尝试以某种不同的方式锁定元素:

按类别获取:

QWebElement button = webView->page()->mainFrame()->documentElement().findFirst("button[class=submit1]");

尝试锁定父级,然后获取第一个子级:

QWebElement parent = **get the parent element**
button = parent.findAll("button")[0]

通常我会尽量避免使用这样的方法,因为它在某些情况下很容易崩溃。如果这是唯一对您有用的方法,我强烈建议您进行一些“尝试\异常(exception)”以避免任何问题。

small note: you said that if u get an element instead of a button, click() works fine. the reason is that click() does nothing to an element that isn't clickable or has some kind of behavior attached to the click() event. So its working by basically doing nothing :)

关于javascript - QWebElement评估JavaScript this.click()方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25717789/

24 4 0
文章推荐: javascript - 事件处理程序和