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
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")
#...
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()
The textbox and button elements are within an <iframe>
so you have to:
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:
Here you can find a relevant discussion on Ways to deal with #document under iframe
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.