在Python3.6版本之后,在selenium和firefox有些不兼容
如果你出现的这样的错误`
>>> browser = webdriver.Firefox('C:\\Program Files (x86)\\Mozilla Firefox')
Traceback (most recent call last):
File "C:\Users\lxm123456\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\lxm123456\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 743, in __init__
restore_signals, start_new_session)
File "C:\Users\lxm123456\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1087, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "#12>", line 1, in
browser = webdriver.Firefox('C:\\Program Files (x86)\\Mozilla Firefox')
File "C:\Users\lxm123456\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 152, in __init__
self.service.start()
File "C:\Users\lxm123456\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
所以我们需要去下载一个geckodriver.exe,然后放在你python.exe
selenium模块
>>> from selenium import webdriver
>>> browser = webdriver.Firefox()
>>> type(browser)
<class 'selenium.webdriver.firefox.webdriver.WebDriver'>
>>> browser.get('http://www.baidu.com')
>>> from selenium import webdriver
>>> browser = webdriver.Firefox()
>>> browser.get('http://inventwithpython.com')
>>> try:
elem = browser.find_element_by_class_name('cover-thumb')
print('我们发现了一个元素带有类名为 cover-thumb,它的标签是%s'%(elem.tag_name))
except:
print('什么都没找到')
我们发现了一个元素带有类名为 cover-thumb,它的标签是img
>>> try:
elem = browser.find_element_by_class_name('card-img-top')
print('我们发现了一个元素带有类名为 card-img-top,它的位置是%s'%(elem.location))
except:
print('什么都没找到')
我们发现了一个元素带有类名为 card-img-top,它的位置是{'x': 200, 'y': 586}
>>> from selenium import webdriver
>>> browser = webdriver.Firefox()
>>> browser.get('http://inventwithpython.com')
>>> linkElem = browser.find_element_by_link_text('More Info')
>>> type(linkElem)
<class 'selenium.webdriver.firefox.webelement.FirefoxWebElement'>
>>> linkElem.click()
解决Selenium弹出新页面无法定位元素问题(Unable to locate element)
>>> browser = webdriver.Firefox()
>>> browser.get('http://www.baidu.com')
>>> browser.find_element_by_id('kw').send_keys('CSDN')
>>> browser.find_element_by_id('su').click()
>>> browser.find_element_by_partial_link_text('专业IT技术社区').click()
>>> browser.switch_to_window(browser.window_handles[1])#移动句柄
>>> browser.find_element_by_link_text('登录').click()
>>> browser.find_element_by_name('username').send_keys('15735184252')
>>> browser.find_element_by_class_name('pass-word').send_keys('pass-word')
>>> browser.find_element_by_class_name('pass-word').submit()
所以>>> browser.switch_to_frame('x-URS-iframe')
很多人在群里问,这个下拉框定位不到、那个弹出框定位不到…各种定位不到,其实大多数情况下就是两种问题:1 有frame,2 没有加等待。殊不知,你的代码运行速度是什么量级的,而浏览器加载渲染速度又是什么量级的,就好比闪电侠和凹凸曼约好去打怪兽,然后闪电侠打完回来之后问凹凸曼你为啥还在穿鞋没出门?凹凸曼分分中内心一万只羊驼飞过,欺负哥速度慢,哥不跟你玩了,抛个异常撂挑子了。
Python selenium —— 一定要会用selenium的等待,三种等待方式解读
By.xpath(“html/body/div/form/input”)
By.xpath(“//input//div”)
By.xpath(“//input[4]”)
By.xpath(“//input[@id=’kw1’]”)
By.xpath(“//input[@type=’name’ and @name=’kw1’]”)
By.xpath(“//input[start-with(@id,’nice’)]”)
By.xpath(“//input[ends-with(@id,’很漂亮’)]”)
By.xpath(“//input[contains(@id,’那么美’)]”)
By.xpath(“//input[@id=’kw1’]//input[start-with(@id,’nice’]/div[1]/form[3])
>>> from selenium import webdriver
>>> browser = webdriver.Firefox()
>>> browser.get('http://mail.163.com')
>>> from selenium.webdriver.common.keys import Keys
>>> browser.get('https://mail.163.com')
>>> browser.switch_to_frame('x-URS-iframe')
>>> browser.find_element_by_name('email')
"67d7fb2f-52bc-4bc8-92f7-63bf121aa5f0", element="dbeee225-9ee5-40a5-8163-382c9a641253")>
>>> browser.find_element_by_name('email').clear()
>>> browser.find_element_by_name('email').send_keys('qmeng1128')
>>> browser.find_element_by_name('password').send_keys('xxxxx',Keys.ENTER)
Keys.DOWN,Keys.UP,Keys.LEFT,Keys.RIGHT 上下左右
Keys.HOME,Keys.END Keys.PAGE_UP,Keys,PAGE_DOWN
Keys.TAB
send_keys(Keys.BACK_SPACE) 删除键
send_keys(Keys.SPACE) 空格键
send_keys(Keys.TAB) 制表键
send_keys(Keys.ESPACE) 回退键/Esc
send_keys(Keys.ENTER) 回车键/换行
send_keys(Keys.CONTROL,'a') 全选
send_keys(Keys.CONTROL,'c') 复制
send_keys(Keys.CONTROL,'x') 剪切
send_keys(Keys.CONTROL,'v') 粘贴
send_keys(Keys.F1) F1
browser.back()
browser.forward()
browser.refresh()
browser.quit()