danwagnerco / selenium-vba

Automatically exported from code.google.com/p/selenium-vba
1 stars 0 forks source link

how to get parentnode and childnodes ? #30

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Operating system :
.Net Framework version :
Office Version :2010
SeleniumWrapper version : latest

What is your issue ?
Hello ,

how can i get the parentnode and childnodes with seleniumWrapper ?

for example how can i do this 

document.getElementById("Account_body").getElementsByTagName("a")[2].parentNode;

or

document.getElementById("Account_body").getElementsByTagName("a")[2]childNodes;

Thanks.

Original issue reported on code.google.com by alam...@gmail.com on 14 Oct 2013 at 12:16

GoogleCodeExporter commented 9 years ago
For the parent, it should be possible with an XPath selector :
WebElement parent = myElement.findElementByXPath("..");

For the child I recommend to select it in the selector for better performance :
Debug.Print  driver.findElementByCssSelector("#Account_body > 
a:nth-child(2)").getAttribute("href");

You can also select the child in the result array :
Debug.Print driver.findElementsByCssSelector("#Account_body > 
a")(2).getAttribute("href")

Or handle each element in a loop :
Dim element
For Each element In driver.findElementsByCssSelector("#Account_body > a")
    Debug.Print element.getAttribute("href")
Next

Original comment by florentbr on 14 Oct 2013 at 2:30

GoogleCodeExporter commented 9 years ago
hi , the problem is that the page where i go is dynamic , therefore , I cannot 
use the xpath , 

to give you an example of what i am doing , here is my javascript code

for(var i=0;i<u.length;i++)
{
if(u[i].innerText=="steve" && 
u[i].parentNode.parentNode.getElementsByTagName("td")[1].innerText=="Fsa" )
{
u[i].getAttribute("data-seclki");
}
}

Original comment by alam...@gmail.com on 14 Oct 2013 at 3:16

GoogleCodeExporter commented 9 years ago
I forgot to mention this
var u=document.getElementById('Account_body').getElementsByTagName('a');

Original comment by alam...@gmail.com on 14 Oct 2013 at 3:18

GoogleCodeExporter commented 9 years ago
I've tested the executeScript commande on the .Net framework and it's possible 
to use WebElements as input and as output. I managed to make it work with the 
VBA wrapper on my version and it will be available in the next release.
Here is an example in csharp with your javascript code with "Steve" and "Fsa" 
as input and a list of WebElements as output :

var elements = (IWebElement[])driver.executeScript(
  "var u=document.getElementById('Account_body').getElementsByTagName('a'); var elements = [];" +
  "for(var i=0;i<u.length;i++)" +
  "  if(u[i].innerText==arguments[0] && u[i].parentNode.parentNode.getElementsByTagName('td')[1].innerText==arguments[1] ) elements.push(u[i]); " +
  "return elements;"
, new []{ "Steve", "Fsa"});

Original comment by florentbr on 14 Oct 2013 at 8:32

GoogleCodeExporter commented 9 years ago
Ok , Great , please let me know when this is available.
Thanks

Original comment by alam...@gmail.com on 14 Oct 2013 at 8:49

GoogleCodeExporter commented 9 years ago
executeScript is fixed in version 1.0.15.0 and it can now take and return 
WebElements.

Examples :

driver.Start "firefox", "http://www.google.com"
driver.Open "/"

Set args = driver.findElement(By.cssselector("a"))
Set rets = driver.executeScript("return arguments[0];", args)
Debug.Print rets.getAttribute("href")

args = driver.findElements(By.cssselector("a"))
rets = driver.executeScript("return arguments;", args)
Debug.Print rets(0).getAttribute("href")

rets = driver.executeScript("return 'apple';")
Debug.Print rets

rets = driver.executeScript("return ['tower', 'bing', 'hello'];")
Debug.Print rets(2)

Original comment by florentbr on 18 Oct 2013 at 7:13

GoogleCodeExporter commented 9 years ago
thanks for this , how do you define args , i get an error message when i run 
the code , please see the screenshot attached.
Thanks

Original comment by alam...@gmail.com on 18 Oct 2013 at 1:25

Attachments:

GoogleCodeExporter commented 9 years ago
Without knowing the line, i'm guessing you forgot to instantiate the By.
Here is the full working code :

Public Sub TC_executescript()

    Dim driver As New SeleniumWrapper.WebDriver
    Dim By As New By

    driver.Start "firefox", "http://www.google.com"
    driver.setImplicitWait 5000
    driver.Open "/"

    Set args = driver.findElement(By.cssselector("a"))
    Set rets = driver.executeScript("return arguments[0];", args)
    Debug.Print rets.getAttribute("href")

    args = driver.findElements(By.cssselector("a"))
    rets = driver.executeScript("return arguments;", args)
    Debug.Print rets(0).getAttribute("href")

    rets = driver.executeScript("return 'apple';")
    Debug.Print rets

    rets = driver.executeScript("return ['tower', 'bing', 'hello'];")
    Debug.Print rets(2)
End Sub

Original comment by florentbr on 18 Oct 2013 at 5:33