Python + Selenium JS下拉列表选择

时间:2018-11-26 19:35:56

标签: python selenium selenium-webdriver xpath css-selectors

我是一个全新的人,我已经浏览了一些示例,您可以在这里找到这些示例,但是看起来很简单,但我无法使其正常工作。

我要通过的页面:www.webauto.de

我的代码来选择品牌,型号并单击“搜索”。

browser = webdriver.Firefox()
browser.get('http://www.webauto.de')
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'quick-search')))
select = Select(browser.find_element_by_id('carsearchmake'))
select.select_by_visible_text('Ford')
sleep(1)
select = Select(browser.find_element_by_id('carsearchmod'))
select.select_by_visible_text('Fiesta')
browser.find_element_by_xpath('//input[@type="submit"]').click()
sleep(1)

2 个答案:

答案 0 :(得分:0)

好像你很近。

xpath

browser.find_element_by_xpath('//input[@type="submit"]')

不能唯一地标识所需的 search 按钮,您可以使用以下解决方案:

browser.find_element_by_xpath("//a[@href='https://www.webauto.de/site/de/suchen/' and contains(.,'Erweiterte Suche')]//following::a[1]/input").click()

答案 1 :(得分:0)

我使用了一个attribute = value CSS选择器来定位提交按钮。

from selenium import webdriver

d = webdriver.FireFox()
d.get("https://www.webauto.de/")
d.find_element_by_xpath("//select[@id='carsearchmake']/option[text()='Ford']").click()
d.find_element_by_xpath("//select[@id='carsearchmod']/option[text()='Fiesta']").click()
d.find_element_by_css_selector("[value=Suchen]").click()
#d.quit()