0

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.

Community
  • 1
  • 1
Dung
  • 19,199
  • 9
  • 59
  • 54

2 Answers2

0

Here,I will give you code. Please check it.

# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome("chromedriver.exe")
driver.get("https://sjobs.brassring.com/TGWebHost/searchopenings.aspx?partnerid=25222&siteid=5011")
ele = driver.find_element_by_xpath("//option[contains(text(),'Calgary  ')]")
print ele
driver.execute_script("arguments[0].scrollIntoView()",ele)
time.sleep(2)
ele.click()
Piyush
  • 511
  • 4
  • 13
  • I got this error "selenium.common.exceptions.ElementNotVisibleException: Message: Element is not visible" at exactly line of "ele.click()" - and that is the point of which we are dealing with headless browser. I am not sure why you gave ("chromedriver.exe") on a Linux environment but I had to remove the entire line and replace with "driver = webdriver.Firefox(capabilities=firefox_capabilities)". Can you please look into it? – Dung Oct 26 '16 at 17:05
  • i am working on windows not Linux and in windows it is working perfect – Piyush Oct 27 '16 at 04:40
  • As you can see, I was asking for headless Linux specifically. But thanks any how for your time. – Dung Oct 28 '16 at 15:59
0

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()
Dung
  • 19,199
  • 9
  • 59
  • 54