单击使用硒的伪元素

编程入门 行业动态 更新时间:2024-10-28 02:28:13
本文介绍了单击使用硒的伪元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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>帐户< / span> < / button> ::之后< / em>

上面的元素就是这样。对象的左侧是 button元素,而:after元素是带有箭头的右侧,当单击该箭头时,它们会下拉下拉菜单。如您所见,右侧没有任何标识符,这在某种程度上使它难以执行。

使用JavaScript在Selenium WebDriver中定位伪元素

这里是一个我的尝试:

string脚本=返回window.getComputedStyle(document.querySelector('#ext-gen33'),':之前'); IJavaScriptExecutor js =(IJavaScriptExecutor)Session.Driver; js.ExecuteScript( arguments [0] .click();,script);

其中出现此错误:

System.InvalidOperationException:'未知错误:arguments [0] .click不是函数(会话信息:chrome = 59.0.3071.115)(驱动程序信息: chromedriver = 2.30.477700(0057494ad8732195794a7b32078424f92a5fce41),platform = Windows NT 6.1.7601 SP1 x86_64)'

I还尝试使用Selenium中的Actions类将鼠标移至左侧,类似于此答案也一样我认为可能是因为我不知道偏移量是多少,并且文档似乎也没有提供任何指示。我认为它是以像素为单位的?

Actions build = new Actions(Session.Driver); build.MoveToElement(FindElement(By.Id( ext-gen33)))。MoveByOffset(235,15).Click()。Build()。Perform();

此尝试似乎在某处单击,因为它没有错误,但是我不确定在哪里。

如果有帮助,我正在尝试在c#中自动化Salesforce(服务云)。

也许有人可以提供解决方案?

解决方案

我在为Salesforce编写Selenium测试时遇到了相同的问题,并通过使用操作直接控制鼠标来解决该问题

此按钮的包装表的硬编码宽度为250px,您已经发现了。要找到鼠标的位置,可以使用 contextClick()方法代替 Click()。它模拟鼠标右键按钮,它将始终打开浏览器菜单。

如果您这样做:

Actions build = new Actions(Session.Driver); build.MoveToElement(FindElement(By.Id( ext-gen33)))。ContextClick()。Build()。Perform();

您会发现鼠标移动到WebElement的中间,而不是左上角(我认为它也是如此)。由于该元素的宽度是恒定的,因此我们可以将鼠标右移 250/2-1 即可,它会起作用:) 代码:

Actions build = new Actions(Session.Driver); build.MoveToElement(FindElement(By.Id( ext-gen33)))。MoveByOffset(124,0).Click()。Build()。Perform();

I am trying to use Selenium to click on a ::after pseudo element. I realize that this cannot be done through the WebDriver directly, but cannot seem to figure out a way to do so with Javascript.

Here is what the DOM looks like:

<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>

This is what the above element looks like. The Left hand side of the object is the 'button' element and the :after element is the right hand side with the arrow which would bring down a dropdown menu when clicked. As you can see that the right hand side has no identifiers whatsoever and that is partially what is making this difficult to do.

I have seen these two links in stackoverflow and have attempted to combine the answers to form my solution, but to no avail.

Clicking an element in Selenium WebDriver using JavaScript Locating pseudo element in Selenium WebDriver using JavaScript

Here is one my attempts:

string script = "return window.getComputedStyle(document.querySelector('#ext-gen33'),':before')"; IJavaScriptExecutor js = (IJavaScriptExecutor) Session.Driver; js.ExecuteScript("arguments[0].click(); ", script);

In which I get this error:

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)'

I've also tried using the Actions class in Selenium to move the mouse in reference to the left hand side, similar to this answer as well. I think it may be because I don't know what the offset is measured in and the documentation doesn't seem to give any indication. I think it is in pixels??

Actions build = new Actions(Session.Driver); build.MoveToElement(FindElement(By.Id("ext-gen33"))).MoveByOffset(235, 15).Click().Build().Perform();

This attempt seems to click somewhere as it gives no errors, but I'm not really sure where.

I'm attempting to automate Salesforce (Service Cloud) in c# if that helps.

Maybe someone can offer a solution?

解决方案

I've encounter the same problem while writing Selenium tests for Salesforce and managed to solve it by direct control over mouse using Actions.

Wrapper table for this button has hardcoded width of 250px, and you have spotted that. To locate where the mouse is, you can use contextClick() method instead of Click(). It simulates right mouse button so it will always open browser menu.

If you do:

Actions build = new Actions(Session.Driver); build.MoveToElement(FindElement(By.Id("ext-gen33"))).ContextClick().Build().Perform();

you will spot that mouse moves to the middle of the WebElement, not the top left corner (I thought that it does too). Since that element width is constant, we can move mouse just by 250 / 2 - 1 to the right and it will work :) code:

Actions build = new Actions(Session.Driver); build.MoveToElement(FindElement(By.Id("ext-gen33"))).MoveByOffset(124, 0).Click().Build().Perform();

更多推荐

单击使用硒的伪元素

本文发布于:2023-11-27 20:42:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   元素

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!