Javascript 在<;p>;使用硒标记
我们使用SeleniumWebDriver实现测试自动化。这是我的要求 HTML看起来像这样Javascript 在<;p>;使用硒标记,javascript,html,selenium,xpath,Javascript,Html,Selenium,Xpath,我们使用SeleniumWebDriver实现测试自动化。这是我的要求 HTML看起来像这样 <p> I need to click before this. Help me achieve this </p> 我需要在此之前单击。帮我做到这一点 “p”标记中的文本可以有任意行数。我得到一个特定的单词作为测试输入,需要将光标放在该单词之前 我尝试使用XPath包含文本来定位元素,它返回段落的中间段落和点击(对于Chrome)。 有人能帮我如何做到这一点吗?在JavaS
<p> I need to click before this. Help me achieve this </p>
我需要在此之前单击。帮我做到这一点
“p”标记中的文本可以有任意行数。我得到一个特定的单词作为测试输入,需要将光标放在该单词之前
我尝试使用XPath包含文本来定位元素,它返回段落的中间段落和点击(对于Chrome)。
有人能帮我如何做到这一点吗?在JavaScript中,您可以使用 在C#和Java中,您可以使用类
使用getSize函数,然后划分元素的高度和宽度,然后应用Action类click方法,下面是示例代码:
WebElement el = driver.findElement(By.xpath("//xpath"));
Dimension location = el.getSize();
int y = location.height/2;
int x = location.width/2;
Actions build = new Actions(driver);
build.moveToElement(el, x, y).click().build().perform();
希望这能奏效 Selenium没有直接处理文本节点的API。 但是,您可以使用一段JavaScript检索单词位置,并通过提供相对于保存文本的元素的偏移位置,使用Actions类单击上一个单词 这是一个双击“Exchange”前面的单词(即“堆栈”)的示例:
//获取元素中单词的相对位置/大小的脚本
最后一个字符串JS\u GET\u WORD\u RECT=
var ele=arguments[0],word=arguments[1],rg=document.createRange()+
对于(var c=ele.firstChild,i;c;c=c.nextSibling){+
“如果(c.nodeType!=3 | |(i=c.nodeValue.indexOf(word))<0)继续;”+
rg.setStart(c,i);rg.setEnd(c,i+word.length)+
var r=ele.getBoundingClientRect(),rr=rg.getClientRects()[0]+
返回{left:(rr.left-r.left)| 0,top:(rr.top-r.top)| 0+
“宽度:rr.width|0,高度:rr.height|0};”+
"};";
WebDriver驱动程序=新的ChromeDriver();
JavascriptExecutor js=(JavascriptExecutor)驱动程序;
//加载页面
驱动程序。获取(“http://stackexchange.com/legal/content-policy");
//获取文本元素
WebElement=driver.findElement(By.cssSelector(“.sectionheader>h2:n个子项(1)”);
//获取单词“Exchange”的相对位置/大小{left,top,width,height}
Map rect=(Map)js.executeScript(js_GET_WORD_rect,元素,“Exchange”);
//为上一个单词“Stack”定义一个相对切点
长偏移量x=(长)矩形获取(“左”)-(长)矩形获取(“宽”)/2;
长偏移量y=(长)矩形获取(“顶部”)+(长)矩形获取(“高度”)/2;
//双击“堆栈”一词
新操作(驱动程序)
.moveToElement(元素,偏移量x.intValue(),偏移量y.intValue())
.doubleClick()
.perform();
driver.quit();
共享更多HTML codeGuy,在标记中查找文本的x,y坐标是一项挑战。
WebElement element = driver.findElement(By...);
Actions actions = new Actions(driver);
actions.moveToElement(element).moveByOffset(dx, dy).click().perform();
WebElement el = driver.findElement(By.xpath("//xpath"));
Dimension location = el.getSize();
int y = location.height/2;
int x = location.width/2;
Actions build = new Actions(driver);
build.moveToElement(el, x, y).click().build().perform();
// script to get the relative position/size of a word in an element
final String JS_GET_WORD_RECT =
"var ele=arguments[0], word=arguments[1], rg=document.createRange(); " +
"for(var c=ele.firstChild, i; c; c=c.nextSibling){ " +
" if(c.nodeType != 3 || (i=c.nodeValue.indexOf(word)) < 0) continue; " +
" rg.setStart(c, i); rg.setEnd(c, i + word.length); " +
" var r = ele.getBoundingClientRect(), rr = rg.getClientRects()[0]; " +
" return { left: (rr.left-r.left) | 0, top: (rr.top-r.top) | 0, " +
" width: rr.width | 0, height: rr.height | 0 }; " +
"};";
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
// load the page
driver.get("http://stackexchange.com/legal/content-policy");
// get the text element
WebElement element = driver.findElement(By.cssSelector(".sectionheader > h2:nth-child(1)"));
// get the relative position/size {left, top, width, height} for the word "Exchange"
Map rect = (Map)js.executeScript(JS_GET_WORD_RECT, element, "Exchange");
// define a relative ckick point for the previous word "Stack"
Long offset_x = (long)rect.get("left") - (long)rect.get("width") / 2;
Long offset_y = (long)rect.get("top") + (long)rect.get("height") / 2;
// double click the word "Stack"
new Actions(driver)
.moveToElement(element, offset_x.intValue(), offset_y.intValue())
.doubleClick()
.perform();
driver.quit();