1

The selection of Calgary in Canadian Cities list does not work, it will always return All cities in the search result after clicking search button pro grammatically. Here is my code:

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

Thanks in advance!

Dung
  • 19,199
  • 9
  • 59
  • 54

1 Answers1

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

Hope this helps

thebadguy
  • 2,092
  • 1
  • 22
  • 31
  • I tested it, It works. Thank you sir, I buy you a beer. – Dung Oct 14 '16 at 20:40
  • @Dung - I always get Calgary when I run your code. Not sure what the problem is in your case. The answer just gives code without telling why the error occurred in the first place. He converted the web element into a select and then interacted with it. – MasterJoe Oct 14 '16 at 23:40
  • @testerjoe2 try my code in question, i used driver:element:click() that does not work that is why he said "Selected Calgary by visible text" and he used Select:select:select_by_visible_text and that works! – Dung Oct 15 '16 at 14:05
  • @Dung - Its supposed to select only Calgary, right ? Your code actually does that for me. Am I missing something here ? – MasterJoe Oct 15 '16 at 18:02
  • @testerjoe2 Right! it is good that it works for you, not sure why it did not for me. May be it is versioning thing. – Dung Oct 16 '16 at 03:45