如何使用Selenium在JavaScript网站上查找元素?

时间:2020-04-07 08:58:44

标签: javascript python selenium automation webdriverwait

我想为自己自动化一些搜索内容,但是这里有些问题。 在此网站上:

https://shop.orgatop.de/

程序找不到搜索栏,我也不知道为什么。

driver = webdriver.Firefox()
driver.get('https://shop.orgatop.de/')
input_search = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="solrSearchTerm"]')))
input_search.click()
input_search.send_keys('asd')
input_search.send_keys(Keys.RETURN)

1 个答案:

答案 0 :(得分:1)

该元素位于innerFrame>catalog>content>input之类的嵌套iframe中。您需要先切换这些框架才能访问输入搜索框。

得出WebDriverWait()和frame_to_be_available_and_switch_to_it()

driver = webdriver.Firefox()
driver.get('https://shop.orgatop.de/')
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"innerFrame")))
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"catalog")))
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"content")))
input_search = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="solrSearchTerm"]')))
input_search.click()
input_search.send_keys('asd')
input_search.send_keys(Keys.RETURN)

浏览器快照:

enter image description here

相关问题