简体   繁体   English

无法使用Selenium WebDriver和Python访问特定网站上的文本框和按钮

[英]Unable to access textbox and button on specific website using Selenium WebDriver and Python

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) 同样的问题,如果我尝试通过xPath或ID访问它们...网站是: http//www.dsv.de/schwimmen/wettkampf-national/schwimmerabfrage/ (德国游泳协会数据库)

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 全部部分位于<iframe>标记内,您需要先切换到它

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 . 您尝试定位元素是不成功的,因为它们嵌套在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. 在尝试单击或以任何方式使用它之前,必须告诉硒切换到包含所需元素的iframe 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: 文本框和按钮元素位于<iframe>因此您必须:

  • Induce WebDriverWait for the desired frame to be available and switch to it . 诱导WebDriverWait获得所需的帧并切换到该帧
  • Induce WebDriverWait for the desired element to be clickable . 诱导WebDriverWait 以获得可点击的所需元素
  • 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 在这里,您可以找到有关iframe下#document处理方式的相关讨论

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM