简体   繁体   English

在硒中的SVG标签中选择g

[英]Selecting g inside SVG tag in selenium

I have just started working in selenium and stuck at some point and need help from experts. 我刚开始从事硒工作,并处于某个阶段,需要专家的帮助。

Here is my html 这是我的html

<div id='d3_tree'>
   <svg>
     <g transform="translate(20,50)>
        <g class='node'>
        </g> 
        <g class='node pe_node'>
        </g>
        <g class='node pe_node'>
        </g> 
     </g>
   </svg>
</div>

I need to have all the <g> having class pe_node and invoke context menu on these <g> I have tried to get the svg like this 我需要让所有<g>具有pe_node类,并在这些<g>上调用上下文菜单,我试图像这样获取svg

node = self.driver.find_elements(By.XPATH, "//div[@id='d3_tree']/'svg']/g")

then I have read that svg can not be selected directly So I tried this 那么我已经读过svg不能直接选择,所以我尝试了

nodes = self.driver.find_elements(By.XPATH, "//div[@id='d3_tree']/*[name()='svg']/g")

and

  nodes = self.driver.find_elements(By.XPATH, "//div[@id='d3_tree']/*[local-name()='svg']/g")

But it is still not working for me and I am getting [] in result. 但是它仍然对我不起作用,并且我得到了[]结果。

Can anyone guide me how to select the <g> with class pe_node inside svg 谁能指导我如何在svg中使用pe_node类选择<g>

Any help will be appreciated 任何帮助将不胜感激

Thanks 谢谢

You were halfway there, the following should work: 您在其中途,以下应该可以工作:

nodes = self.driver.find_elements(By.XPATH, "//div[@id='d3_tree']/*[name()='svg']/*[name()='g']")

Every element inside 'svg' has to be referenced as `/*[name()=''] 'svg'中的每个元素都必须引用为`/ * [name()='']

In this case you can shorten it up a bit with: 在这种情况下,您可以使用以下方法将其缩短:

nodes = self.driver.find_elements(By.XPATH, "//div[@id='d3_tree']/*/*[name()='g']")

跟随xpath应该可以//div[@id='d3_tree']//g[contains(@class, 'pe_node')]

Could you not select the <svg> element using the tagName ? 您不能使用tagName选择<svg>元素吗?

node = driver.findElement(By.tagName("svg"))
otherNodes = node.findElements(By.Xpath("./g[contains(@class, 'pe_node')]")

You can try this,I am unaware of which language you are using.But the below selenium will might be helpful for you.Nodes will return all the elements under svg tag with having class as "node pe_node". 您可以尝试一下,我不知道您使用的是哪种语言。但是下面的硒元素可能对您有所帮助。节点将返回svg标签下的所有元素,且其类为“ node pe_node”。

node = self.driver.find_element(By.XPATH, "//div[@id='d3_tree']/svg]")
nodes = node.find_elements(By.XPATH,"//g[@class='node pe_node']")

您可以这样写:

//div[@id='d3_tree']/*[name()='svg']/*[name()='g' and @class='node pe_node']/*[name()='g'][2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM