【发布时间】:2018-08-30 18:49:45
【问题描述】:
我正在尝试使用 SVG 节点自动化测试。我需要点击一个特定的节点。
我在 Firefox 中单击该元素,单击 F12 以启动控制台,然后键入 console.dir($0) 以查看详细信息。这是元素的样子:
text
__data__: Object { height: 0, depth: 1, x: 0, … }
attributes: NamedNodeMap [ fill="#fff", font-size="36px", font-family="mapicons", … ]
baseURI: "https://fnord.co.uk/#/jobs/add/job?@=-702x6714391y19z&lf=1!19.73740"
childElementCount: 0
childNodes: NodeList [ #text ]
children: HTMLCollection []
classList: DOMTokenList []
className: SVGAnimatedString { baseVal: "", animVal: "" }
clientHeight: 0
clientLeft: 0
clientTop: 0
clientWidth: 0
dataset: DOMStringMap { }
dx: SVGAnimatedLengthList { baseVal: SVGLengthList, animVal: SVGLengthList }
dy: SVGAnimatedLengthList { baseVal: SVGLengthList, animVal: SVGLengthList }
farthestViewportElement: <svg id="svgstruct" style="position: relative; overflow: visible; margin-top: 50px; margin-left: 20px; min-width: 100%;" width="100%" height="100%">
firstChild: #text ""
firstElementChild: null
id: ""
innerHTML: "\ue05e"
isConnected: true
lastChild: #text ""
lastElementChild: null
lengthAdjust: SVGAnimatedEnumeration { baseVal: 1, animVal: 1 }
localName: "text"
namespaceURI: "http://www.w3.org/2000/svg"
nearestViewportElement: <svg id="svgstruct" style="position: relative; overflow: visible; margin-top: 50px; margin-left: 20px; min-width: 100%;" width="100%" height="100%">
nextElementSibling: null
nextSibling: null
nodeName: "text"
nodeType: 1
nodeValue: null
onabort: null
onanimationcancel: null
onanimationend: null
onanimationiteration: null
onanimationstart: null
onauxclick: null
onblur: null
oncanplay: null
oncanplaythrough: null
onchange: null
onclick: null
onclose: null
oncontextmenu: null
oncopy: null
oncut: null
ondblclick: null
ondrag: null
ondragend: null
ondragenter: null
ondragexit: null
ondragleave: null
ondragover: null
ondragstart: null
ondrop: null
ondurationchange: null
onemptied: null
onended: null
onerror: null
onfocus: null
ongotpointercapture: null
oninput: null
oninvalid: null
onkeydown: null
onkeypress: null
onkeyup: null
onload: null
onloadeddata: null
onloadedmetadata: null
onloadend: null
onloadstart: null
onlostpointercapture: null
onmousedown: null
onmouseenter: null
onmouseleave: null
onmousemove: null
onmouseout: null
onmouseover: null
onmouseup: null
onmozfullscreenchange: null
onmozfullscreenerror: null
onpaste: null
onpause: null
onplay: null
onplaying: null
onpointercancel: null
onpointerdown: null
onpointerenter: null
onpointerleave: null
onpointermove: null
onpointerout: null
onpointerover: null
onpointerup: null
onprogress: null
onratechange: null
onreset: null
onresize: null
onscroll: null
onseeked: null
onseeking: null
onselect: null
onselectstart: null
onshow: null
onstalled: null
onsubmit: null
onsuspend: null
ontimeupdate: null
ontoggle: null
ontransitioncancel: null
ontransitionend: null
ontransitionrun: null
ontransitionstart: null
onvolumechange: null
onwaiting: null
onwebkitanimationend: null
onwebkitanimationiteration: null
onwebkitanimationstart: null
onwebkittransitionend: null
onwheel: null
outerHTML: "<text fill=\"#fff\" font-size=\"36px\" font-family=\"mapicons\" transform=\"translate(0,30)\">\ue05e</text>"
ownerDocument: HTMLDocument https://alloy-labs.yotta.co.uk/#/jobs/add/job?@=-702x6714391y19z&lf=1!19.73740
ownerSVGElement: <svg id="svgstruct" style="position: relative; overflow: visible; margin-top: 50px; margin-left: 20px; min-width: 100%;" width="100%" height="100%">
parentElement: <g class="node" cursor="pointer" transform="translate(30,110)">
parentNode: <g class="node" cursor="pointer" transform="translate(30,110)">
prefix: null
previousElementSibling: <path d="M0,0a30,30,0,1,0,0,60h0a30,30,0,1,0,0,-60Z" fill="#f5c041">
previousSibling: <path d="M0,0a30,30,0,1,0,0,60h0a30,30,0,1,0,0,-60Z" fill="#f5c041">
requiredExtensions: SVGStringList { length: 0, numberOfItems: 0 }
requiredFeatures: SVGStringList { length: 0, numberOfItems: 0 }
rotate: SVGAnimatedNumberList { baseVal: SVGNumberList, animVal: SVGNumberList }
scrollHeight: 0
scrollLeft: 0
scrollLeftMax: 0
scrollTop: 0
scrollTopMax: 0
scrollWidth: 0
style: CSS2Properties { }
systemLanguage: SVGStringList { length: 0, numberOfItems: 0 }
tabIndex: -1
tagName: "text"
textContent: "\ue05e"
textLength: SVGAnimatedLength { baseVal: SVGLength, animVal: SVGLength }
transform: SVGAnimatedTransformList { baseVal: SVGTransformList […], animVal: SVGTransformList […] }
viewportElement: <svg id="svgstruct" style="position: relative; overflow: visible; margin-top: 50px; margin-left: 20px; min-width: 100%;" width="100%" height="100%">
x: SVGAnimatedLengthList { baseVal: SVGLengthList, animVal: SVGLengthList }
y: SVGAnimatedLengthList
animVal: SVGLengthList { numberOfItems: 0, length: 0 }
baseVal: SVGLengthList
length: 0
numberOfItems: 0
__proto__: SVGLengthListPrototype { clear: clear(), initialize: initialize(), getItem: getItem(), … }
__proto__: SVGAnimatedLengthListPrototype { baseVal: Getter, animVal: Getter, … }
__proto__: SVGTextElementPrototype
constructor: ()
length: 0
name: "SVGTextElement"
prototype: SVGTextElementPrototype { … }
Symbol(Symbol.hasInstance): undefined
__proto__: function ()
__proto__: SVGTextPositioningElementPrototype
我尝试使用按类名查找,但没有成功:
String className="SVGAnimatedString";
List<WebElement> cardTitles = driver.findElements(By.className(className));
显然className不代表这种className?
所以我尝试使用 cssSelector 找到它。这可行,但不太有用,因为每次屏幕加载时 CSS 选择器都会更改。 找到我的元素后,如果我只使用 element.click 单击它,则不会发生任何事情。 如果我使用这个 Javascript 点击它,它会抛出一个异常:
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
异常信息:
org.openqa.selenium.JavascriptException: TypeError: arguments[0].click 不是函数
如何让点击在此元素上起作用?
这不是 Selenium WebDriver [Java]: How to Click on elements within an SVG using XPath 的副本
与那个询问者不同,我设法找到了我的元素。
我的问题是点击那个元素。
浏览器:Firefox Quantum 59.0.1(64 位) 壁虎司机:0.19.1 硒罐:3.8.1
【问题讨论】:
-
我不明白@DebanjanB 是怎么回事。我没有使用 xpath 作为初学者!
-
@DebanJanB。根本不是同一个问题!
-
@DebanjanB。你真的读过我的问题吗?
标签: javascript java css selenium svg