1

I'm trying to use selenium to type in data in inputboxes. But I cant get any element (NoSuchElementException). Problem is only with this site.

I tried searching by name/id but it failed.

WebDriver driver = new FirefoxDriver();        
driver.get("https://ekrs.ms.gov.pl/web/wyszukiwarka-krs/strona-glowna");
System.out.println(driver.getCurrentUrl());
System.out.println("Successfully opened the website");
WebElement wb = driver.findElement(By.id("rejestrPrzedsiebiorcy"));

My goal (for now) is just to get this element :P.

Kurohige
  • 1,378
  • 2
  • 16
  • 24
  • _...cant get any element..._ which element are you trying to access? – undetected Selenium Dec 29 '18 at 14:14
  • Possible duplicate of [Find elements inside forms and iframe using Java and Selenium WebDriver](https://stackoverflow.com/questions/24247490/find-elements-inside-forms-and-iframe-using-java-and-selenium-webdriver) – JeffC Dec 29 '18 at 15:06

3 Answers3

3

The problem is the form is included in the source via <iframe> element. You can see that it has attribute src="https://ekrs.ms.gov.pl/krsrdf/krs/wyszukiwaniepodmiotu?". If you go to that link, you will see standalone form. The question is - how to access an included source? It's quite simple ;)

  1. Find the <iframe> element:

    WebElement frame = driver.findElement(By.xpath("//div[@class='portlet-body']/div/iframe"))

  2. Switch to that frame:

    driver.switchTo().frame(frame)

And that's it! Now you are in the <iframe> element context, and you can search inside it. so this will work now:

WebElement wb = driver.findElement(By.id("rejestrPrzedsiebiorcy"));

To switch back (get out of the frame context) you just have to call:

driver.switchTo().defaultContent();

Note that this site has dynamically generated ids to prevent automation, and uses CaptchaV3 (you can see I used xpath expression to find the iframe). Selenium is easily detectable if you are not careful.

xinaiz
  • 7,744
  • 6
  • 34
  • 78
  • It worked. Thx dude :D. Just one more thing. When i press SearchButton ("Szukaj") it pops up a window "please enter proper code". Do You know what might be the cause? – Konstanty Maciej Lachowicz Dec 29 '18 at 14:36
  • 1
    @KonstantyMaciejLachowicz You probably need to fill `Numer KRS:` input or something else. I don't know the internal logic.EDIT tested, not working. – xinaiz Dec 29 '18 at 14:45
  • 1
    I think ReCaptcha blocks selenium actions. For example, if you use `element.click()`, selenium "teleports" cursor to that element. It's detected, and the site blocks you from using it. Take a look at [org.openqa.selenium.interactions.Actions](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html). You need to "slow down selenium". But that's all just a guess. And it might be impossible or really hard. – xinaiz Dec 29 '18 at 14:55
  • 1
    Nevermind, ReCaptcha just detects if you are using WebDriver, so there is no salvation. – xinaiz Dec 29 '18 at 15:06
  • 1
    TO ANYONE INTERESTED: I dodged ReCaptcha by clearing cache and cookies before clicking "search" button. – Konstanty Maciej Lachowicz Dec 29 '18 at 16:48
  • @KonstantyMaciejLachowicz I checked the cookies, and it's enough to delete one: `driver.manage().deleteCookieNamed("BIGipServerPOOL_S24_KRS_PCL")`. No need to remove all of them. – xinaiz Dec 29 '18 at 17:09
1

Carefully observe the HTML code after inspecting required element. If you element is inside <iframe> then you need to switch on to frame first and then find that element.

below is the way to switch to frame:

driver.switchTo().frame() method takes one of the three possible arguments:

A number.

Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index 0, the second at index 1 and the third at index 2. Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.

driver.switchTo().frame(0)

A name or ID.

Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.

driver.switchTo().frame("name here");

A previously found WebElement.

Select a frame using its previously located WebElement.

    WebElement iframeElement = driver.findElement(By.id("IF1"));

    //now use the switch command
    driver.switchTo().frame(iframeElement);
murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

Try driver.findElement(By.id("form-main")); first, then driver.findElement(By.id("rejestrPrzedsiebiorcy")); again.

Minh Doan
  • 37
  • 7