【发布时间】:2018-01-07 16:40:45
【问题描述】:
我正在尝试使用 Selenium 单击 ::after 伪元素。我意识到这不能直接通过 WebDriver 完成,但似乎无法找到使用 Javascript 的方法。
这是 DOM 的样子:
<em class="x-btn-split" unselectable="on" id="ext-gen161">
<button type="button" id="ext-gen33" class=" x-btn-text">
<div class="mruIcon"></div>
<span>Accounts</span>
</button>
::after
</em>
这就是上面的元素的样子。对象的左侧是 'button' 元素, :after 元素是右侧的箭头,单击时会弹出一个下拉菜单。如您所见,右侧没有任何标识符,这部分是导致此操作变得困难的部分原因。
我在 stackoverflow 中看到了这两个链接,并尝试将答案组合起来形成我的解决方案,但无济于事。
Clicking an element in Selenium WebDriver using JavaScript
Locating pseudo element in Selenium WebDriver using JavaScript
这是我的尝试之一:
string script = "return window.getComputedStyle(document.querySelector('#ext-gen33'),':before')";
IJavaScriptExecutor js = (IJavaScriptExecutor) Session.Driver;
js.ExecuteScript("arguments[0].click(); ", script);
我得到这个错误:
System.InvalidOperationException: 'unknown error: arguments[0].click is not a function
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 6.1.7601 SP1 x86_64)'
我还尝试使用 Selenium 中的 Actions 类来参考左侧移动鼠标,类似于this answer。我认为这可能是因为我不知道偏移量是多少,并且文档似乎没有给出任何指示。我认为是像素??
Actions build = new Actions(Session.Driver);
build.MoveToElement(FindElement(By.Id("ext-gen33"))).MoveByOffset(235, 15).Click().Build().Perform();
这个尝试似乎点击了某个地方,因为它没有给出错误,但我不确定在哪里。
如果有帮助,我正在尝试在 c# 中自动化 Salesforce(服务云)。
也许有人可以提供解决方案?
【问题讨论】:
-
点击伪元素附加到的元素
-
我们可以尝试另一种方法,例如图像识别向下箭头并单击。我们可以使用 sikuli 来完成它。我们需要下载库并使用它。Screen scr = new Screen(); Pattern Image1 = new Pattern("D:/>SikuliImages/SignupClick.png"); scr.Click(Image1, true);其中 image1 是裁剪后的下拉菜单。
-
@guest271314 我试过了,但没那么简单,然后按钮的左侧被点击了,这就是我的问题。我需要点击右侧。
-
@Jand 据我所知,不可能以编程方式将
DOM事件分派给 CSS 伪元素或伪类。 -
@santhoshkumar 有趣的解决方案。Sikuli 网站没有列出它是用 c# 编写的。
标签: javascript c# selenium pseudo-element