[英]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 . 注意:我可以选择option,并且我的代码可以在非无头环境Windows机器上工作,这是python selenium-webdriver select option不起作用 。 I am now dealing with production headless Ubuntu hence the browser is not really opened on any physical display.
我现在正在处理无头生产的Ubuntu,因此实际上没有在任何物理显示器上打开浏览器。
Thanks again in advance. 再次感谢。
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()
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. 答案是使用PhantomJS无头Webkit浏览器,它将以完全相同的代码在Window和Linux上运行。 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.