I can’t find an element for Id

Asked

Viewed 836 times

2

I’m automating an operation on a website with Selenium Webdriver and in a particular region of the site when sending a wait using the WebDriverWait it exceeds the maximum time and does not find, but I have checked several times the Id and that’s correct, this problem had already happened in other regions of the site, but I was able to solve by giving a driver.SwitchTo().Frame("NomeDoIframe"), that is to switch to the iframe where the elements were, but now when I do this it gives error saying that can not find the iframe. I’m using the browser Phantomjs.

  • Is there any way to find Xpath? CSS? Minimal Xpath? Name? or something more consistent? Sometimes not the same account ID... I always prefer Xpath and CSS.

  • I use Xpath a lot too, I was trying with it, put the path and in one of the elements put the Id, I’ll see if there are other parameters I can use, vlw

3 answers

0

After evaluating the processed page, do a Document.getElementById:

 var page = require('webpage').create();
 page.open('http://www.meusite.org', function(status) {
   if (status !== 'success') {
     console.log('Sem conexão');
   } else {
     var ua = page.evaluate(function() {
       return document.getElementById('idBotao').textContent;
     });
     console.log(ua);
   }
   phantom.exit();
 });

If you want to use the Jquery selector do a lib include first:

page.includeJs(pathJquery, function() 
{
     var res = page.evaluate(function () 
     {
        $('#obj').text();
     }
}

http://phantomjs.org/page-automation.html

0

To switch to Frame with an Id, you need to pass the command as follows:

driver.SwitchTo().Frame(driver.FindElement(By.Id("Id_Do_Seu_Iframe")));

0

As mentioned earlier you can do the driver.SwitchTo().Frame(driver.FindElement(By.Id("id_do_frame1"))); , however in certain cases where the DOM has already changed you may need to navigate back to the element. If the iframe navigation does not work you may need to do:

    driver.SwitchTo().DefaultContent();
    driver.SwitchTo().Frame(driver.FindElement(By.Id("id_do_frame1")));
    driver.SwitchTo().Frame(driver.FindElement(By.Id("id_do_frame2")));

if you have several chained frames.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.