Dung Dung - 1 month ago 17
Python Question

selenium webdriver select an option in dropdown menu using python on headless Linux with firefox browser

I have this code:

#!/usr/bin/env python
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import Select
from pyquery import *
# declaration of variables
display = Display(visible=0, size=(800, 600))
display.start()
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
# Initialize
driver = webdriver.Firefox(capabilities=firefox_capabilities)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get('https://sjobs.brassring.com/TGWebHost/searchopenings.aspx?partnerid=25222&siteid=5011')
print driver.title
# below does not work
# driver.find_element_by_xpath(".//*[@id='Question4138__FORMTEXT62']/option[37]").click()
# selectsoptions = driver.find_element_by_id("Question4138__FORMTEXT62")
# for option in selectsoptions .find_elements_by_tag_name('option'):
# if option.text == 'Calgary':
# option.select()
# break
driver.find_element_by_id('ctl00_MainContent_submit1').click()
# call a sub-routine function def (not shown here)
save_rows(driver.find_element_by_id('idSearchresults'))
driver.close()
display.stop()


the output:

"Search Jobs - Walmart Canada Careers"

The problem is that I do not know how to select "Calgary" in field "Canadian Cities". I have tried many different ways but still it does not work. Can you please help with?

Note: I am able to select option and my code works in a Non-headless environment Windows machine, here it is python selenium-webdriver select option does not work. I am now dealing with production headless Ubuntu hence the browser is not really opened on any physical display.

Thanks again in advance.

Answer

Tested Solution:

The answer to this is using PhantomJS the Headless Webkit browser, it will work on both Window and Linux with the exact same code. Here is example:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from pyquery import *
import json
import csv
import sys
import time

def save_rows(elements):
    rows = elements.find_element_by_id('idSearchresults_dataBody')
    for row in rows.find_elements_by_tag_name('tr'):
        link = row.find_element_by_css_selector('a').get_attribute('href')
        print link

driver = webdriver.PhantomJS(service_args=['--ssl-protocol=any'])
driver.implicitly_wait(10)
driver.get('https://sjobs.brassring.com/TGWebHost/searchopenings.aspx?partnerid=25222&siteid=5011')

text = "Calgary"
currentselection = driver.find_element_by_id("Question4138__FORMTEXT62")
select = Select(currentselection)
select.deselect_by_visible_text("All")
select.select_by_visible_text(text)

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

save_rows(driver.find_element_by_id('idSearchresults'))

driver.quit()