如果您想了解使用pythonseleniumchromedriver从源中选择隐藏的选项值和selenium获取隐藏元素的知识,那么本篇文章将是您的不二之选。我们将深入剖析使用pythonseleni
如果您想了解使用python selenium chromedriver从源中选择隐藏的选项值和selenium获取隐藏元素的知识,那么本篇文章将是您的不二之选。我们将深入剖析使用python selenium chromedriver从源中选择隐藏的选项值的各个方面,并为您解答selenium获取隐藏元素的疑在这篇文章中,我们将为您介绍使用python selenium chromedriver从源中选择隐藏的选项值的相关知识,同时也会详细的解释selenium获取隐藏元素的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- 使用python selenium chromedriver从源中选择隐藏的选项值(selenium获取隐藏元素)
- 3-Python爬虫-动态HTML/Selenium+PhantomJS/chrome无头浏览器-chromedriver
- centos7.4 64位安装 google-chrome 与 chromedriver 运行 Python selenium 项目
- ChromeDriver仅在BMP错误中支持字符,同时使用Selenium Python将带有ChromeDriver Chrome的Emoji发送给Tkinter的label()文本框
- Chrome浏览器通过Pythonselenium自动更新时如何使用特定版本的ChromeDriver
使用python selenium chromedriver从源中选择隐藏的选项值(selenium获取隐藏元素)
我正在阅读Docx文件这里是[链接],从中解析一些文本,然后使用pythonselenium绑定和chrome-
driver我试图从源(driver.page_source)中单击“隐藏”选项值。我知道无法选择。到目前为止,这是我的代码:
import time, refrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom docx import opendocx, getdocumenttextfrom requests import Sessiondef read_return(word_file): document = opendocx(word_file) paratextlist = getdocumenttext(document) newparatextlist = [] for paratext in paratextlist: newparatextlist.append((paratext.encode("utf-8")).strip(''\n'').strip(''\t'').strip(''\r'')) newparatextlist = str(newparatextlist).replace("]","").replace("[","") with open(''sample.txt'',''wb'')as writer: writer.write(newparatextlist) return newparatextlistword_file = read_return(''Taxatierapport SEK - Auto Centrum Bollenstreek - Peugeot 308 - 5603.docx'')x = lambda x:re.findall(x,word_file,re.DOTALL)[0].strip().replace("''","'").replace(''"'',''''')Voertuig = x("::OBJECT::'', ''(.+?)''")Merk = x("::MERK::'', ''(.+?)''")Model = x("::TYPE::'', ''(.+?)''")TOELATING = x("::BOUWJAAR 1STE TOELATING::'', ''(.+?)''")d1 = TOELATING.split("-")[0]d2 = TOELATING.split("-")[1]d3 = TOELATING.split("-")[2]TRANSMISSIE = x("::TRANSMISSIE::'', ''(.+?)''")BRANDSTOF = x("::BRANDSTOF::'', ''(.+?)''")print "%r\n%r\n%r\n%r\n%r\n%r\n%r\n%r\n" %(Voertuig, Merk, Model, d1, d2, d3, TRANSMISSIE, BRANDSTOF)if Voertuig == "Personenauto": value = 1elif Voertuig == "Personenbussen": value = 7elif Voertuig == "Bedrijfsauto's tot 3.5 ton": value = 3elif Voertuig == "Bedrijfsauto's 4x4": value = 2elif Voertuig == "Motoren": value= 5xr = 0; yr = 0; zr = 1972while xr < 32: if int(d1) == xr: dvalue1 = xr else: pass xr+=1while yr < 13: if int(d2) == yr: dvalue2 = yr else: pass yr+=1while zr < 2018: if int(d3) == zr: dvalue3 = zr else: pass zr+=1driver = webdriver.Chrome(''chromedriver.exe'')driver.get(''https://autotelexpro.nl/LoginPage.aspx'')driver.find_element(By.XPATH, value =''//*[@id="ctl00_cp_LogOnView_LogOn_txtVestigingsnummer"]'').send_keys(''3783'')driver.find_element(By.XPATH, value =''//*[@id="ctl00_cp_LogOnView_LogOn_txtGebruikersnaam"]'').send_keys(''Frank'')driver.find_element(By.XPATH, value =''//*[@id="ctl00_cp_LogOnView_LogOn_Password"]'').send_keys(''msnauto2016'')driver.find_element(By.XPATH, value =''//*[@id="ctl00_cp_LogOnView_LogOn_btnLogin"]'').click()time.sleep(10)#try:driver.find_element(By.XPATH, value =''//select[@name="ctl00$cp$ucSearch_Manual$ddlVoertuigType"]/option[@value="''+str(value)+''"]'').click()driver.find_element(By.XPATH, value =''//select[@name="ctl00$cp$ucSearch_Manual$ddlBouwdag"]/option[@value="''+str(dvalue1)+''"]'').click()driver.find_element(By.XPATH, value =''//select[@name="ctl00$cp$ucSearch_Manual$ddlBouwmaand"]/option[@value="''+str(dvalue2)+''"]'').click()driver.find_element(By.XPATH, value =''//select[@name="ctl00$cp$ucSearch_Manual$ddlBouwjaar"]/option[@value="''+str(dvalue3)+''"]'').click()driver.find_element(By.XPATH, value =''//select[@name="ctl00$cp$ucSearch_Manual$ddlMerk"]/option[@value="130"]'').click()#except:driver.quit()time.sleep(5)driver.quit()
所以使用请求模块,我向链接发出POST请求并设法获得包含所需选项数据的响应,请参见此处:
<select name="ctl00$cp$ucSearch_Manual$ddlMerk" onchange="updateInputForServerNoPB();InvalidateVehicleSearchResult();setTimeout('__doPostBack(\'ctl00$cp$ucSearch_Manual$ddlMerk\',\'\')', 0)" id="ctl00_cp_ucSearch_Manual_ddlMerk"> <option selected="selected" value="-1">- Kies merk -</option> <option value="95">Alfa Romeo</option> <option value="154">Aston Martin</option> <option value="96">Audi</option> <option value="97">Bentley</option> <option value="98">BMW</option> <option value="352">Bugatti</option> <option value="100">Cadillac</option> <option value="342">Chevrolet</option> <option value="101">Chevrolet USA</option> <option value="102">Chrysler</option> <option value="103">Citroen</option> <option value="337">Corvette</option> <option value="104">Dacia</option> <option value="105">Daihatsu</option> <option value="166">Daimler</option> <option value="162">Dodge</option> <option value="106">Donkervoort</option> <option value="107">Ferrari</option> <option value="108">Fiat</option> <option value="94">Ford</option> <option value="111">Honda</option> <option value="340">Hummer</option> <option value="112">Hyundai</option> <option value="365">Infiniti</option> <option value="113">Jaguar</option> <option value="114">Jeep</option> <option value="150">Kia</option> <option value="115">Lada</option> <option value="116">Lamborghini</option> <option value="117">Lancia</option> <option value="168">Land Rover</option> <option value="432">Landwind</option> <option value="118">Lexus</option> <option value="119">Lotus</option> <option value="120">Maserati</option> <option value="330">Maybach</option> <option value="121">Mazda</option> <option value="122">Mercedes-Benz</option> <option value="304">Mini</option> <option value="124">Mitsubishi</option> <option value="126">Morgan</option> <option value="127">Nissan</option> <option value="128">Opel</option> <option value="130">Peugeot</option> <option value="132">Porsche</option> <option value="134">Renault</option> <option value="135">Rolls-Royce</option> <option value="138">Saab</option> <option value="139">Seat</option> <option value="140">Skoda</option> <option value="226">smart</option> <option value="343">Spyker</option> <option value="210">SsangYong</option> <option value="141">Subaru</option> <option value="142">Suzuki</option> <option value="417">Think</option> <option value="144">Toyota</option> <option value="147">Volkswagen</option> <option value="145">Volvo</option> </select>
,我想知道是否可以将上述字符串文本添加到 driver.page_source中 ,以便可以使用 驱动程序 属性遍历选项值?
答案1
小编典典from selenium import webdriverfrom selenium.webdriver.common.by import Byimport timefrom selenium.webdriver.support.ui import Selectdriver = webdriver.Chrome()driver.maximize_window()driver.get(''https://autotelexpro.nl/LoginPage.aspx'')driver.find_element(By.XPATH, value =''//*[@id="ctl00_cp_LogOnView_LogOn_txtVestigingsnummer"]'').send_keys(''3783'')driver.find_element(By.XPATH, value =''//*[@id="ctl00_cp_LogOnView_LogOn_txtGebruikersnaam"]'').send_keys(''Frank'')driver.find_element(By.XPATH, value =''//*[@id="ctl00_cp_LogOnView_LogOn_Password"]'').send_keys(''msnauto2016'')driver.find_element(By.XPATH, value =''//*[@id="ctl00_cp_LogOnView_LogOn_btnLogin"]'').click()time.sleep(10)currentselection = driver.find_element_by_xpath(".//*[@id=''ctl00_cp_ucSearch_Manual_ddlVoertuigType'']")select = Select(currentselection)select.select_by_visible_text("Motoren")time.sleep(5)try: x=driver.find_element_by_xpath(".//*[@id=''ctl00_cp_ucSearch_Manual_ddlBouwdag'']") select = Select(x) select.select_by_visible_text("1") y=driver.find_element_by_xpath(".//*[@id=''ctl00_cp_ucSearch_Manual_ddlBouwmaand'']") select = Select(y) select.select_by_visible_text("1") z=driver.find_element_by_xpath(".//*[@id=''ctl00_cp_ucSearch_Manual_ddlBouwjaar'']") select = Select(z) select.select_by_visible_text("2017") time.sleep(5) car = driver.find_element_by_css_selector("#ctl00_cp_ucSearch_Manual_ddlMerk") select = Select(car) select.select_by_visible_text("BTC")except: print "Not able to select"
此代码将有所帮助。看到更好的方法是显式等待,但对于临时解决方案,我使用了time.sleep()
更新:如果要从汽车下拉列表中获取选项,可以使用以下方法:
def getallcarlist(): currentselection = driver.find_element_by_xpath(".//*[@id=''ctl00_cp_ucSearch_Manual_ddlVoertuigType'']") select = Select(currentselection) select.select_by_visible_text("Motoren") time.sleep(5) x = driver.find_element_by_xpath(".//*[@id=''ctl00_cp_ucSearch_Manual_ddlBouwdag'']") select = Select(x) select.select_by_visible_text("1") y = driver.find_element_by_xpath(".//*[@id=''ctl00_cp_ucSearch_Manual_ddlBouwmaand'']") select = Select(y) select.select_by_visible_text("1") z = driver.find_element_by_xpath(".//*[@id=''ctl00_cp_ucSearch_Manual_ddlBouwjaar'']") select = Select(z) select.select_by_visible_text("2017") time.sleep(5) car = driver.find_element_by_css_selector("#ctl00_cp_ucSearch_Manual_ddlMerk") carlist =[] for option in car.find_elements_by_tag_name(''option''): carlist.append((option.text).encode(''utf8'')) return carlist
这是称呼它的方式
listcar= getallcarlist()for c in listcar: print c
输出将是:
- Kies merk -AGMAJPApriliaBenelliBetaBMWBTCBullitDerbiDucatiEnergicaGileraHarley DavidsonHeskethHondaHusqvarnaHyosungIndianKawasakiKTMKymcoLongjiaMashMorganMorsMoto GuzziMV AgustaNimotoOssaPeugeotPiaggioQuadroRazzoRenaultRoyal EnfieldSachsScomadiSuzukiSWMSYMTriumphTurbhoVespaVictoryVolta MotorbikesYamahaYibenZero Motorcycles
3-Python爬虫-动态HTML/Selenium+PhantomJS/chrome无头浏览器-chromedriver
动态HTML
爬虫跟反爬虫
动态HTML介绍
- JavaScrapt
- jQuery
- Ajax
- DHTML
- Python采集动态数据
- 从Javascript代码入手采集
- Python第三方库运行JavaScript,直接采集你在浏览器看到的页面
Selenium + PhantomJS
- Selenium: web自动化测试工具
- 自动加载页面
- 获取数据
- 截屏
- 安装: pip install selenium==2.48.0
- 官网: http://selenium-python.readthedocs.io/index.html
- PhantomJS(幽灵)
- 基于Webkit 的无界面的浏览器
- 官网: http://phantomjs.org/download.html
- Selenium 库有有一个WebDriver的API
- WebDriver可以跟页面上的元素进行各种交互,用它可以来进行爬取
- 案例 v36
- chrome + chromedriver
- 下载安装chrome: 下载+安装
- 下载安装chromedriver:
- Selenium操作主要分两大类:
- 得到UI元素
- find_element_by_id
- find_elements_by_name
- find_elements_by_xpath
- find_elements_by_link_text
- find_elements_by_partial_link_text
- find_elements_by_tag_name
- find_elements_by_class_name
- find_elements_by_css_selector
- 基于UI元素操作的模拟
- 单击
- 右键
- 拖拽
- 输入
- 可以通过导入ActionsChains类来做到
- 案例37
- 得到UI元素
案例v36
''''''
通过webdriver操作进行查找1
''''''
from selenium import webdriver
import time
# 通过Keys模拟键盘
from selenium.webdriver.common.keys import Keys
# 操作哪个浏览器就对哪个浏览器建一个实例
# 自动按照环境变量查找相应的浏览器
driver = webdriver.PhantomJS()
# 如果浏览器没有在相应环境变量中,需要指定浏览器位置
driver.get("http://www.baidu.com")
# 通过函数查找title标签
print("Title: {0}".format(driver.title))
案例v37
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
# 可能需要手动添加路径
driver = webdriver.Chrome()
url = "http://www.baidu.com"
driver.get(url)
text = driver.find_element_by_id(''wrapper'').text
print(text)
print(driver.title)
# 得到页面的快照
driver.save_screenshot(''index.png'')
# id="kw" 的是百度的输入框,我们得到输入框的ui元素后直接输入“大熊猫"
driver.find_element_by_id(''kw'').send_keys(u"大熊猫")
# id="su"是百度搜索的按钮,click模拟点击
driver.find_element_by_id(''su'').click()
time.sleep(5)
driver.save_screenshot("daxiongmao.png")
#获取当前页面的cookie
print(driver.get_cookies())
# 模拟输入两个按键 ctrl+ a
driver.find_element_by_id(''kw'').send_keys(Keys.CONTROL, ''a'')
#ctr+x 是剪切快捷键
driver.find_element_by_id(''kw'').send_keys(Keys.CONTROL, ''x'')
driver.find_element_by_id(''kw'').send_keys(u''航空母舰'')
driver.save_screenshot(''hangmu.png'')
driver.find_element_by_id(''su'').send_keys(Keys.RETURN)
time.sleep(5)
driver.save_screenshot(''hangmu2.png'')
# 清空输入框 , clear
driver.find_element_by_id(''kw'').clear()
driver.save_screenshot(''clear.png'')
# 关闭浏览器
driver.quit()
selenium自动化测试 工具:
-用selenium登录的时候,先要get到页面; -get到页面之后就可以准备输入了, -selenium可以模拟输入; -selenium调没有界面的chrome或者PhantomJS -保存快照,手动输入,对静态验证码的一大杀手。
centos7.4 64位安装 google-chrome 与 chromedriver 运行 Python selenium 项目
centos7.4 实例
利用 yum 命令安装 google-chrome 超级简单(安装最新版):
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
chromedriver 也下载最新版就好,和Mac 版本一样,我依旧选择这个最新版本:
http://chromedriver.storage.googleapis.com/index.html?path=70.0.3538.16/
Python3.7 之前安装过:
Centos7 环境下 Python2.7 换成 Python3.7 运行 scrapy 应用所遇到的问题记录
然后配置一下,就能跑起来 selenium 项目了,当然该 pip install xxx 安装模块就缺什么安装什么了
# 设置 webdriver 参数
options = webdriver.ChromeOptions()
# 设置谷歌浏览器的一些选项
# proxy 代理 options 选项
options.add_argument(r''--proxy--server=http:\\'' + ip)
uas = get_uas()
# 设置user-agent
options.add_argument(''user-agent='' + choice(uas))
# 以 headless 方案运行
options.add_argument(''--headless'')
options.add_argument(''--no-sandbox'')
# options.add_argument(''--disable-dev-shm-usage'')
# 禁用图片访问
# prefs = {"profile.managed_default_content_settings.images": 2}
# options.add_experimental_option("prefs", prefs)
如果不配置 # 以 headless 方案运行
会抛出下面异常
[root@iZwz9117d4ssv2v5oe81m3Z selenium_baidu]# python testRun.py
package_util 初始化
testRun.py:32: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(path, chrome_options=options)
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/python/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/python/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "testRun.py", line 32, in get_url
driver = webdriver.Chrome(path, chrome_options=options)
File "/usr/python/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/usr/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/python/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn''t exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=70.0.3538.16 (16ed95b41bb05e565b11fb66ac33c660b721f778),platform=Linux 3.10.0-693.2.2.el7.x86_64 x86_64)
selenium 代码正确运行:
ChromeDriver仅在BMP错误中支持字符,同时使用Selenium Python将带有ChromeDriver Chrome的Emoji发送给Tkinter的label()文本框
如何解决ChromeDriver仅在BMP错误中支持字符,同时使用Selenium Python将带有ChromeDriver Chrome的Emoji发送给Tkinter的label()文本框?
这个错误讯息…
selenium.common.exceptions.WebDriverException: Message: unkNown error: ChromeDriver only supports characters in the BMP
…暗示ChromeDriver无法 通过方法发送表情符号信号。 send_keys()
ChromeDriver仅支持BMP中的字符,这是Chromium团队的一个已知问题,因为ChromeDriver仍不支持 Unicode字符。因此,它是无法发送任何字符以外通过ChromeDriver。结果,任何发送SMP字符(例如CJK,Emojis,Symbols等)的尝试都会引发错误。
Alternative
一个潜在的替代方法是使用_GeckoDriver_ / Firefox.
-
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import webdriverwait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path=r’C:\Utility\browserDrivers\geckodriver.exe’) driver.get(''https://www.google.com/’)
Chineese Character
webdriverwait(driver, 20).until(EC.element_to_be_clickable((By.NAME, “q”))).send_keys(“
解决方法
我正在自动化whatsapp消息,并希望通过
tkinter窗口发送出去。在此tkinter窗口中,我
借助.label()创建了一个消息框,并能够通过硒连接到whatsapp网站。目前,我已经可以发送消息了,但是没有表情符号。当我
包含表情符号时,出现此错误“ Chromedriver仅支持
BMP中的字符”。如何包含表情符号?
Chrome浏览器通过Pythonselenium自动更新时如何使用特定版本的ChromeDriver
如何解决Chrome浏览器通过Pythonselenium自动更新时如何使用特定版本的ChromeDriver?
,除了更新 ChromeDriver 二进制版本外,没有其他选择,而 Chrome浏览器 会不断自动更新。
原因
在添加,修改和删除现有功能后,会释放每个 Chrome浏览器 。为了符合当前的 浏览器功能 集, Chrome团队 会不时发布兼容的ChromeDriver 二进制文件。这些 ChromeDriver 二进制文件能够与 Chrome浏览器 进行交互。某些版本的ChromeDriver 二进制文件支持特定范围的 Chrome浏览器 版本(某些是最新版本),如下所示:
-
ChromeDriver v (2020-05-28)
Supports Chrome version 84
-
ChromeDriver v (2020-05-05)
Supports Chrome version 83
-
ChromeDriver v 被 故意 跳过。
-
ChromeDriver v (2020-05-05)
Supports Chrome version 81
-
ChromeDriver v (2020-02-13)
Supports Chrome version 80
-
ChromeDriver v (2019-11-18)
Supports Chrome version 79
-
ChromeDriver v (2019-10-21)
Supports Chrome version 78
-
ChromeDriver v (2019-08-20)
Supports Chrome version 77
-
ChromeDriver v (2019-08-20)
Supports Chrome version 76
-
ChromeDriver v (2019-04-29)
Supports Chrome version 75
-
ChromeDriver v (2019-03-14)
Supports Chrome version 74
-
ChromeDriver v (2019-03-06)
Supports Chrome version 73
-
ChromeDriver v (2019-02-01)
Supports Chrome v71-73
-
ChromeDriver v (2018-12-10)
Supports Chrome v70-72
-
ChromeDriver v (2018-11-19)
Supports Chrome v69-71
-
ChromeDriver v (2018-10-16)
Supports Chrome v69-71
-
ChromeDriver v (2018-09-13)
Supports Chrome v68-70
-
ChromeDriver v (2018-07-27)
Supports Chrome v67-69
-
ChromeDriver v (2018-06-07 )
Supports Chrome v66-68
-
ChromeDriver v (2018-05-30)
Supports Chrome v66-68
-
ChromeDriver v (2018-04-17 )
Supports Chrome v65-67
-
ChromeDriver v (2018-03-16 )
Supports Chrome v64-66
-
ChromeDriver v (2018-03-02 )
Supports Chrome v63-65
-
ChromeDriver v (2018-01-10 )
Supports Chrome v62-64
-
ChromeDriver v (2017-12-10 )
Supports Chrome v61-63
-
ChromeDriver v (2017-10-03)
Supports Chrome v60-62
-
ChromeDriver v (2017-08-30)
Supports Chrome v59-61
-
ChromeDriver v (2017-07-21)
Supports Chrome v58-60
-
ChromeDriver v (2017-06-07)
Supports Chrome v58-60
-
ChromeDriver v (2017-04-04)
Supports Chrome v56-58
结论
要使脚本/程序与更新的 Chrome浏览器 保持交互,您必须根据 兼容性将ChromeDriver 二进制版本与 Chrome浏览器 保持同步。
解决方法
我是Selenium的新手,现在可以将selenium与Chromedriver一起使用以进行基本的自动测试,代码可以正常工作,但问题是Chrome浏览器始终在后端自动更新,并且代码始终在Chrome更新后无法运行。我知道我需要下载新的chromedriver来解决此问题,但是我想知道是否有任何方法可以在不禁用chromebrowser更新的情况下解决此问题?tks。
我正在使用Windows 10 / Chrome版本67 / Python 3.6.4 / Selenium 3.12.0
我们今天的关于使用python selenium chromedriver从源中选择隐藏的选项值和selenium获取隐藏元素的分享就到这里,谢谢您的阅读,如果想了解更多关于3-Python爬虫-动态HTML/Selenium+PhantomJS/chrome无头浏览器-chromedriver、centos7.4 64位安装 google-chrome 与 chromedriver 运行 Python selenium 项目、ChromeDriver仅在BMP错误中支持字符,同时使用Selenium Python将带有ChromeDriver Chrome的Emoji发送给Tkinter的label()文本框、Chrome浏览器通过Pythonselenium自动更新时如何使用特定版本的ChromeDriver的相关信息,可以在本站进行搜索。
本文标签: