如何使用Selenium Chrome Web驱动程序自动执行登录凭据 [英] How to automate login credential with selenium chrome web driver

查看:82
本文介绍了如何使用Selenium Chrome Web驱动程序自动执行登录凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从[this] [1]网站提取数据:

I am trying to extract data from the [this][1] website:

手动过程是在搜索框中输入字符串,例如"CCOCCO",单击预测属性",然后从表中记录玻璃化转变温度(K)".

The manual procedure is to enter String such as 'CCOCCO' in the search box , click on "Predict Properties" and record the 'Glass Transition Temperature (K)' from the table.

如果html POST的数量少于5,以下代码将自动执行上述任务:

The following code will automate the above task if the number of html POST is less than 5:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options=Options()
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
driver=webdriver.Chrome(chrome_options=options)

def get_glass_temperature(smiles):
    driver.get('https://www.polymergenome.org/explore/index.php?m=1')
    x_path_click="//input[@class='large_input_no_round ui-autocomplete-input' and @id='keyword_original']"
    x_path_find="//input[@class='dark_blue_button_no_round' and @value='Predict Properties']"
    x_path_get="//table[@class='record']//tbody/tr[@class='record']//following::td[7]/center/font/font"
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x_path_click))).send_keys(smiles)
    driver.find_element_by_xpath(x_path_find).click()
    return WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,x_path_get))).get_attribute("innerHTML")

我将上述函数应用于具有tp 400的String类似于'CCOCCO'的pandas数据帧.但是,在返回5玻璃温度"后,由于网站引发以下消息,将出现WebdriverException错误:

I am applying the above function on a pandas dataframe that has up tp 400 value of String similar to 'CCOCCO'. However, after returning 5 "Glass Temperature" there will be WebdriverException error as the website throws the following message:

"Visits of more than 5 times per day to the property prediction capability requires login. "

在运行代码之前,我登录网站并选中记住我"框,但错误是相同的.

Before running the code, I login to the website and check the "remember me" box but the error is the same.

我尝试如下修改代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd 
import os 

options=Options()
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
driver=webdriver.Chrome(chrome_options=options, executable_path='/Users/ae/Downloads/chromedriver')

def get_glass_temperature(smiles):
    driver.get('https://www.polymergenome.org/explore/index.php?m=1')
    user_name='my_user_name'
    password='my_password'
    x_path_id="//input[@class='large_input_no_round' and @placeholder='User ID']"
    x_path_pass="//input[@class='large_input_no_round' and @placeholder='Password']"
    x_path_sign="//input[@class='orange_button_no_round' and @value='Sign In']"
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x_path_id))).send_keys(user_name)
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x_path_pass))).send_keys(password)
    driver.find_element_by_xpath(x_path_sign).click()

    x_path_click="//input[@class='large_input_no_round ui-autocomplete-input' and @id='keyword_original']"
    x_path_find="//input[@class='dark_blue_button_no_round' and @value='Predict Properties']"
    x_path_get="//table[@class='record']//tbody/tr[@class='record']//following::td[7]/center/font/font"
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x_path_click))).send_keys(smiles)
    driver.find_element_by_xpath(x_path_find).click()
    return WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,x_path_get))).get_attribute("innerHTML")

test_smiles=['CC(F)(F)CC(F)(F)','CCCCC(=O)OCCOC(=O)','CNS-C6H3-CSN-C6H3','CCOCCO','NH-CS-NH-C6H4','C4H8','C([*])C([*])(COOc1cc(Cl)ccc1)']
test_polymer=pd.DataFrame({'SMILES': test_smiles})
test_polymer['test_tg']=test_polymer['SMILES'].apply(get_glass_temperature)
print (test_polymer)

此修改后,我收到超时错误:

After this modification, I am getting TimeOut Error:

Traceback (most recent call last):
  File "/Users/alieftekhari/Desktop/extract_TG.py", line 42, in <module>
    test_polymer['test_tg']=test_polymer['SMILES'].apply(get_glass_temperature)
  File "/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 3194, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/_libs/src/inference.pyx", line 1472, in pandas._libs.lib.map_infer
  File "/Users/user/Desktop/extract_TG.py", line 22, in get_glass_temperature
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x_path_id))).send_keys(user_name)
  File "/anaconda/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
  [1]: https://www.polymergenome.org/explore/index.php?m=1

推荐答案

这是python中的实现:

here is implementation in python:

def time_out_handling (smiles):
        try:
            user_name='my_user_name'
            password='my_password'
            x_path_id="//input[@class='large_input_no_round' and @placeholder='User ID']"
            x_path_pass="//input[@class='large_input_no_round' and @placeholder='Password']"
            x_path_sign="//input[@class='orange_button_no_round' and @value='Sign In']"
            WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x_path_id))).send_keys(user_name)
            WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x_path_pass))).send_keys(password)
            driver.find_element_by_xpath(x_path_sign).click()
            driver.get('https://www.polymergenome.org/explore/index.php?m=1')
            x_path_click="//input[@class='large_input_no_round ui-autocomplete-input' and @id='keyword_original']"
            x_path_find="//input[@class='dark_blue_button_no_round' and @value='Predict Properties']"
            x_path_get="//table[@class='record']//tbody/tr[@class='record']//following::td[7]/center/font/font"
            WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x_path_click))).send_keys(smiles)
            driver.find_element_by_xpath(x_path_find).click()
            return WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,x_path_get))).get_attribute("innerHTML").encode('ascii', 'ignore').split(' ')[0]
        except TimeoutException:
            return "Nan" 


def get_glass_temperature(smiles):
        try: 
            driver.get('https://www.polymergenome.org/explore/index.php?m=1')
            x_path_click="//input[@class='large_input_no_round ui-autocomplete-input' and @id='keyword_original']"
            x_path_find="//input[@class='dark_blue_button_no_round' and @value='Predict Properties']"
            x_path_get="//table[@class='record']//tbody/tr[@class='record']//following::td[7]/center/font/font"
            WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x_path_click))).send_keys(smiles)
            driver.find_element_by_xpath(x_path_find).click()
            return WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,x_path_get))).get_attribute("innerHTML").encode('ascii', 'ignore').split(' ')[0]
        except WebDriverException:
            time_out_handling (smiles)

这篇关于如何使用Selenium Chrome Web驱动程序自动执行登录凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆