百度翻译
看别人发了一个百度翻译的代码:from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
browser = webdriver.Chrome(chrome_options = options)
browser.get('https://fanyi.baidu.com/#auto/zh/')
input_area = browser.find_element_by_css_selector('#baidu_translate_input')
content = input('请输入你需要翻译的内容:')
input_area.send_keys(content)
time.sleep(2)
output_area = browser.find_element_by_css_selector('#main-outer > div > div > \
div.translate-wrap > div.translateio > div.translate-main.clearfix > \
div.trans-right > div > div > div.output-bd > \
p.ordinary-output.target-output.clearfix > span')
print('翻译结果:',output_area.text)
browser.close()
执行结果:Traceback (most recent call last):
File "D:\Python\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "D:\Python\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "D:\Python\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\admin\Desktop\hello.py", line 7, in <module>
browser = webdriver.Chrome(chrome_options = options)
File "D:\Python\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "D:\Python\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: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
求教 要下对应浏览器的版本的驱动,并把它放在 Python 安装目录里,selenium 才会正常工作 下载谷歌浏览器,下载对应的chromedriver,那不是我的代码吗 塔利班 发表于 2018-10-24 18:02
下载谷歌浏览器,下载对应的chromedriver,那不是我的代码吗
确实,我最近在搞百度翻译,看到你这代码直接拿来用了 https://fishc.com.cn/thread-121621-1-1.html你按这个帖子的镜像去下载chromedriver,下载完放到scripts文件夹下 对了,对于谷歌内核的浏览器,代码有效吗? Traceback (most recent call last):
File "C:\Users\admin\Desktop\hello.py", line 7, in <module>
browser = webdriver.Chrome(chrome_options = options)
File "D:\Python\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "D:\Python\lib\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: cannot find Chrome binary
(Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64)
又怎么了(谷歌驱动版本:2.29,2345浏览器谷歌内核:56.0.2924.90) 你没下对版本
下2。39的 还是不行……因此换IE了 你下了那个,然后360软件管家按个谷歌浏览器就行了 代码改了一下:
from selenium import webdriver
import time
content = input('请输入你需要翻译的内容:')
options = webdriver.IeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
browser = webdriver.Ie(options = options)
browser.get('https://fanyi.baidu.com')
input_area = browser.find_element_by_css_selector('#baidu_translate_input')
input_area.send_keys(content)
time.sleep(2)
output_area = browser.find_element_by_css_selector('#main-outer > div > div > \
div.translate-wrap > div.translateio > div.translate-main.clearfix > \
div.trans-right > div > div > div.output-bd > \
p.ordinary-output.target-output.clearfix > span')
print('翻译结果:',output_area.text)
browser.close()
结果:
请输入你需要翻译的内容:hello
Traceback (most recent call last):
File "C:\Users\admin\Desktop\hello.py", line 10, in <module>
input_area = browser.find_element_by_css_selector('#baidu_translate_input')
File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 598, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "D:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with css selector == #baidu_translate_input
IE能正常启动,这是什么情况 @塔利班 你这么好像@不出来我,我也没有提醒,当网页没有加载完毕(你的网速慢的时候可能控件没有加载出来),
需要until那个什么函数,这里用sleep只是人为控制时间然后读取,可能你的网速比较慢,未实现控件加载 from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
这个是selenium 官方的waits文档
参照这个你把ID换成CSS选择器,对应变量名设置下,我没有按IE得驱动,你按文档改下就行 IE加载慢,超时了……代码仍在改进中 请输入你需要翻译的内容:hello
Traceback (most recent call last):
File "C:\Users\admin\Desktop\hello.py", line 13, in <module>
EC.presence_of_element_located((By.ID, "#baidu_translate_input"))
File "D:\Python\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
{:5_95:}
页:
[1]