0

I'm taking my first steps in selenium, and am facing a strange problem right now. I want to navigate on a website and enter text in a search box as well as click the "enter"-button to proceed to the next page. In general, I know how to do this, and it works seamlessly on other websites, but this one seems to cause trouble somehow. When I search the textbox and button by name, it just can't find them. Same problem if I try to access them via xPath or ID... The website is: http://www.dsv.de/schwimmen/wettkampf-national/schwimmerabfrage/ (Database of German Swimming Association)

My code so far looks like the following:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

url = "http://www.dsv.de/schwimmen/wettkampf-national/schwimmerabfrage/"

driver = webdriver.Chrome()
driver.get(url)

submit_button = driver.find_element_by_name("_submitButton")
fistname_textbox = driver.find_element_by_xpath('//*[@id="_firstnameTextBox"]')
lastname_textbox = driver.find_element_by_xpath('//*[@id="_lastnameTextBox"]')
regid_textbox = driver.find_element_by_id("_regidTextBox")

fistname_textbox.send_Keys("String 1")
lastname_textbox.send_keys("String 2")
submit_button.click()

driver.close()

If any of you could help me and find out what causes this problem, I would be super grateful :) I'm getting more and more confused right now

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

4 Answers4

0

The all section is inside <iframe> tag, you need to switch to it first

from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, 5)
wait.until(ec.frame_to_be_available_and_switch_to_it((By.TAG_NAME, 'iframe')))

submit_button = driver.find_element_by_name("_submitButton")
#...
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Thanks for your solution, but I get an Error message: wait.until(ec.frame_to_be_available_and_switch_to_it(By.TAG_NAME, 'iframe')) TypeError: __init__() takes 2 positional arguments but 3 were given – juliu_mbr Mar 17 '19 at 10:26
  • @juliu_mbr Try `wait.until(ec.frame_to_be_available_and_switch_to_it((By.TAG_NAME, 'iframe')))` with double `(())` – Guy Mar 17 '19 at 10:47
0

Your attempts to locate the element are unsuccessful because of they are nested within an iframe. One must tell selenium to switch to the iframe that contains the desired element before attempting to click it or use it in any way. Try the following:

from selenium import webdriver

url = "http://www.dsv.de/schwimmen/wettkampf-national/schwimmerabfrage/"

driver = webdriver.Chrome()
driver.get(url)
driver.switch_to.frame(0);
submit_button = driver.find_element_by_name("_submitButton")
fistname_textbox = driver.find_element_by_xpath('//*[@id="_firstnameTextBox"]')
lastname_textbox = driver.find_element_by_xpath('//*[@id="_lastnameTextBox"]')
regid_textbox = driver.find_element_by_id("_regidTextBox")

fistname_textbox.send_keys("String 1")
lastname_textbox.send_keys("String 2")
submit_button.click()

driver.close()
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

The textbox and button elements are within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      options = webdriver.ChromeOptions()
      options.add_argument('start-maximized')
      options.add_argument('disable-infobars')
      options.add_argument('--disable-extensions')
      driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
      driver.get("http://www.dsv.de/schwimmen/wettkampf-national/schwimmerabfrage/")
      WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@src='https://dsvdaten.dsv.de/Modules/Results/Individual.aspx?Lang=de-DE']")))
      WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='TextBox' and @id='_firstnameTextBox']"))).send_keys("juliu_mbr")
      driver.find_element_by_xpath("//input[@class='TextBox' and @id='_lastnameTextBox']").send_keys("juliu_mbr")
      driver.find_element_by_xpath("//input[@class='Button' and @id='_submitButton']").click()
      
  • Browser Snapshot:

dsv_de

Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You need to switch to frame and then locate the elements

url = "http://www.dsv.de/schwimmen/wettkampf-national/schwimmerabfrage/"

driver = webdriver.Chrome()
driver.get(url)
driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@src='https://dsvdaten.dsv.de/Modules/Results/Individual.aspx?Lang=de-DE']'));

// then your code for the Login

submit_button = driver.find_element_by_name("_submitButton")
fistname_textbox = driver.find_element_by_xpath('//*[@id="_firstnameTextBox"]')
lastname_textbox = driver.find_element_by_xpath('//*[@id="_lastnameTextBox"]')
regid_textbox = driver.find_element_by_id("_regidTextBox")

fistname_textbox.send_Keys("String 1")
lastname_textbox.send_keys("String 2")
submit_button.click()

driver.close()
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36