python selenium-webdriver 选择选项不起作用

标签 python selenium-webdriver drop-down-menu

在加拿大城市列表中选择卡尔加里不起作用,以编程方式单击搜索按钮后,它始终会返回搜索结果中的所有城市。这是我的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

# Initialize
driver = webdriver.Firefox()
driver.get('https://sjobs.brassring.com/TGWebHost/searchopenings.aspx?partnerid=25222&siteid=5011')
# Select city name Calgary
calgaryOptionXpath = ".//*[@id='Question4138__FORMTEXT62']/option[37]"
calgaryOptionElement = WebDriverWait(driver, 10).until(lambda driver:driver.find_element_by_xpath(calgaryOptionXpath))
calgaryOptionElement.click()
# click submit button "Search"
driver.find_element_by_id('ctl00_MainContent_submit1').click()

提前致谢!

最佳答案

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

# Initialize
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
driver.get('https://sjobs.brassring.com/TGWebHost/searchopenings.aspx?partnerid=25222&siteid=5011')



# Select city name Calgary
text = "Calgary"  # what ever you want to select in dropdown
currentselection = driver.find_element_by_id("Question4138__FORMTEXT62")
select = Select(currentselection)
select.select_by_visible_text(text)

select.deselect_by_visible_text("All")

print("Selected Calgary by visible text")

driver.find_element_by_id('ctl00_MainContent_submit1').click()

希望这有帮助

关于python selenium-webdriver 选择选项不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40031592/

相关文章:

python - 用单列中的值替换所有列值 - Pandas

python - python升级后升级pip包

python - 内存错误 Numpy/Python 欧几里得距离

python - 在第一个测试完成且测试失败后,Python和Unittest打开第二个浏览器

php - 打开下拉菜单时更新 MySQL 数据库(注意)

c# - 如何检查组合框下拉列表是向上显示还是向下显示?

python - Django 表单不显示,只有按钮(新)

java - ChromeDriver - 在 Selenium WebDriver 自动化上弹出禁用开发者模式扩展

selenium - Webdriver 获取子项列表并使用 xpath 对其进行过滤

css - 网站上的定位元素有问题