我正在迈出硒的第一步,现在正面临一个奇怪的问题。我想在网站上导航并在搜索框中输入文字,然后单击“输入”按钮进入下一页。总的来说,我知道如何做到这一点,并且它可以在其他网站上无缝运行,但是这似乎在某种程度上引起麻烦。当我按名称搜索文本框和按钮时,只是找不到它们。如果我尝试通过xPath或ID访问它们,也会遇到同样的问题... 该网站是:http://www.dsv.de/schwimmen/wettkampf-national/schwimmerabfrage/(德国游泳协会的数据库)
到目前为止,我的代码如下:
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()
如果你们中的任何一个可以帮助我,找出导致此问题的原因,我将非常感激:)我现在越来越困惑
答案 0 :(得分:0)
所有部分都位于<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")
#...
答案 1 :(得分:0)
您尝试定位元素的尝试失败,因为它们嵌套在import css from './public/someFile.module.css';
...
class App extends Component {
componentDidMount() {
console.log(css.toString();
}
中。在尝试单击或以任何方式使用硒之前,必须告诉硒切换到包含所需元素的iframe
。请尝试以下操作:
iframe
答案 2 :(得分:0)
文本框和按钮元素位于<iframe>
中,因此您必须:
您可以使用以下解决方案:
代码块:
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()
浏览器快照:
在这里您可以找到有关Ways to deal with #document under iframe的相关讨论
答案 3 :(得分:0)
您需要切换到框架,然后找到元素
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()