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.

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

3 Comments

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?
i am working on windows not Linux and in windows it is working perfect
As you can see, I was asking for headless Linux specifically. But thanks any how for your time.
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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.