如何使用JS的executeScript()方法将一个新的xpath替换现有的xpath?

huangapple go评论81阅读模式
英文:

How to replace a new xpath with the existing xpath using JS executeScript() method?

问题

我正在使用Java编写一个Selenium方法,该方法通过在XPath中注入HTML属性'style'来突出显示文本区域,背景为黄色,边框为红色。它通过使用JavascriptExecutor类和executeScript()方法实现。

问题出现在XPath没有属性'style'时。
当遇到这样的XPath(<span dir="auto" class="sapMITHTextContent">All (42936)</span>)时,该方法会添加属性:'style = "background: yellow; border: 2px solid red;"'

新的XPath现在是:
<span dir="auto" class="sapMITHTextContent" style="background: yellow; border: 2px solid red;">All (42936)</span>,并且此按钮以黄色突出显示,并具有红色边界。我看到这样的文本被选择和验证。此突出显示保持500毫秒,然后控制尝试删除此突出显示。现在问题开始了。

当Selenium控制达到注释行时:

js.executeScript("arguments[0].setAttribute('style', '" + originalStyle + "');", element); 这时问题发生了。控制尝试替换style = "background: yellow; border: 2px solid red;"并放入style = "",因为原始样式属性根本不存在。新的XPath现在变成了<span dir="auto" class="sapMITHTextContent" style="">All (42936)</span>

这现在引发了StaleElementReferenceException。

有没有办法在突出显示完成后用原始XPath替换整个XPath?
<span dir="auto" class="sapMITHTextContent" style="background: yellow; border: 2px solid red;">All (42936)</span> ==> <span dir="auto" class="sapMITHTextContent">All (42936)</span>?我尝试过解决方法,但它们效果非常不好。

英文:

I am writing a Selenium method in Java which highlights the text area with a Yellow Background and Red boundary. It does by injecting a HTML Attribute 'style' in the xpath using JavascriptExecutor class and executeScript() method.

public static void objHighlight(WebDriver webdriver, WebElement element)
{
	JavascriptExecutor js = (JavascriptExecutor) webdriver;
	String originalStyle = element.getAttribute("style");
	js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);
	try
	{
		Thread.sleep(500);
	}
	catch (InterruptedException e)
	{
		e.printStackTrace();
	}
	//js.executeScript("arguments[0].setAttribute('style', '" + originalStyle + "');", element);
				
}

The problem comes when an xpath doesnt have the Attribute 'style'.
When such a xpath is encountered (<span dir="auto" class="sapMITHTextContent">All (42936)</span>), the method adds the attribute: 'style = "background: yellow; border: 2px solid red;"'.

The new xpath is now:
<span dir="auto" class="sapMITHTextContent" style="background: yellow; border: 2px solid red;">All (42936)</span> and this button is highlighted in Yellow and has a Red boundary. I see that such and such a text is selected and verified. This highlight stays for 500 ms and then the control tries to remove this highlighting. Now the problem begins.

When the Selenium control reaches the commented line:

js.executeScript("arguments[0].setAttribute('style', '" + originalStyle + "');", element); this is when the problem occurs. The control tries to replace style = "background: yellow; border: 2px solid red;" and puts in style = "" , as the original style attribute was not present at all. The new xpath now becomes <span dir="auto" class="sapMITHTextContent" style="">All (42936)</span>.

This now throws a StaleElementReferenceException.

Is there any way I can replace the whole xpath with the original xpath after the highlighting is done?

<span dir="auto" class="sapMITHTextContent" style="background: yellow; border: 2px solid red;">All (42936)</span> ===>> <span dir="auto" class="sapMITHTextContent">All (42936)</span>? I have tried workarounds, but they are highly ineffective.

答案1

得分: 0

作为初始元素:

<span dir="auto" class="sapMITHTextContent">All (42936)</span>

被修改为:

<span dir="auto" class="sapMITHTextContent" style="background: yellow; border: 2px solid red;">All (42936)</span>

要将元素设置回原始状态,您可以使用以下方法中的 removeAttribute() 如下所示:

public static void objHighlight(WebDriver webdriver, WebElement element)
{
    JavascriptExecutor js = (JavascriptExecutor) webdriver;
    // String originalStyle = element.getAttribute("style");
    js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);
    try
    {
        Thread.sleep(500);
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    js.executeScript("arguments[0].removeAttribute('style')", element);
}
英文:

As the initial element:

<span dir="auto" class="sapMITHTextContent">All (42936)</span>

gets modified as:

<span dir="auto" class="sapMITHTextContent" style="background: yellow; border: 2px solid red;">All (42936)</span>

Now to set the element back the original state you can use removeAttribute() method as follows:

public static void objHighlight(WebDriver webdriver, WebElement element)
{
	JavascriptExecutor js = (JavascriptExecutor) webdriver;
	// String originalStyle = element.getAttribute("style");
	js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);
	try
	    {
		Thread.sleep(500);
	    }
	catch (InterruptedException e)
	    {
		e.printStackTrace();
	    }
	js.executeScript("arguments[0].removeAttribute('style')", element);
}

huangapple
  • 本文由 发表于 2023年7月6日 14:15:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定