天天看点

selenium tips

【Selenium Python】

【Selenium Python WebDriver】

6、去掉链接的新标签页打开属性(target="_blank"):

<code>aEle </code><code>=</code> <code>Driver.find_elements_by_tag_name(</code><code>'a'</code><code>):</code>

<code>Driver.execute_script(</code><code>"arguments[0].removeAttribute('target')"</code><code>, aEle)</code>

【Selenium Python WebElement】

【Selenium Python Firefox】

3、Firefox设置代理:

<code>profile </code><code>=</code> <code>webdriver.FirefoxProfile()</code>

<code>profile.set_preference(</code><code>"network.proxy.type"</code><code>, </code><code>1</code><code>)</code>

<code>profile.set_preference(</code><code>"network.proxy.http"</code><code>, ProxyAddr)</code>

<code>profile.set_preference(</code><code>"network.proxy.http_port"</code><code>, ProxyPort)</code>

<code>Driver </code><code>=</code> <code>webdriver.Firefox(profile)</code>

4、如果不在环境变量中设置驱动路径,可以用executable_path指定驱动文件路径:

<code>webdriver.Firefox(executable_path</code><code>=</code><code>r</code><code>'E:\webdriver\geckodriver.exe'</code><code>)</code>

【Selenium Python Chrome】

3、如果不在环境变量(Path)中设置驱动路径,可以用executable_path指定驱动文件路径:

<code>webdriver.Chrome(executable_path</code><code>=</code><code>r</code><code>'E:\webdriver\chromedriver.exe'</code><code>)</code>

4、selenium3、chorme55下的一些配置。

<code>chromeOptions </code><code>=</code> <code>webdriver.ChromeOptions()</code>

<code>prefs </code><code>=</code> <code>{}</code>

<code>#禁用图片</code>

<code>prefs[</code><code>'profile.managed_default_content_settings.images'</code><code>] </code><code>=</code> <code>2</code>

<code>#设置下载目录</code>

<code>prefs[</code><code>'download.default_directory'</code><code>] </code><code>=</code> <code>'D:/tmp'</code>

<code>chromeOptions.add_experimental_option(</code><code>"prefs"</code><code>,prefs)</code>

<code>browser </code><code>=</code> <code>webdriver.Chrome(chrome_options</code><code>=</code><code>chromeOptions)</code>

【相关阅读】

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1596120如需转载请自行联系原作者

RQSLT