簡體   English   中英

如何遍歷 Excel 工作表以在網頁 Python Selenium 上執行搜索

[英]How can I iterate through an excel sheet to perform a search on a webpage Python Selenium

我想遍歷一個公司列表,逐一搜索它們並保存href。

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
import pandas as pd
from lxml import html 
import time
import requests 
df=pd.read_excel('/Users/ap/companies.xlsx')
browser = Firefox(options=opts)
browser.get('https://webpage')
search_form=browser.find_element_by_id('ctl00_ContentPlaceHolder1_frmEntityName')
i=0
for i in df['company_name']:
    search_form.send_keys(i)
    search_form_buttom=browser.find_element_by_id('ctl00_ContentPlaceHolder1_btnSubmit').click()
#wait a bit to make this element work.search_form.send_keys('BioHealth')
    time.sleep(15)
    i=i+1 

我收到以下錯誤,我無法解決它,甚至無法抓取hrefs。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-68-e157420a273e> in <module>()
     21 #wait a bit to make this element work.search_form.send_keys('BioHealth')
     22     time.sleep(10)
---> 23     i=i+1
     24 

TypeError: coercing to Unicode: need string or buffer, int found 

for 循環將df['company_name']的字符串分配給它的變量i 在循環結束時,您向該字符串添加1 ,這是不允許的,因為 python 解釋器不能將 int 隱式轉換為字符串。

我有一種感覺,您正在嘗試使用i=i+1作為循環計數器變量,但這在 for-each-loop 中不需要(即for i in foo )。 只需刪除i=i+1 循環仍將按預期運行。

但是,如果你真的想給i存儲的字符串添加一個 1,你必須這樣寫: i=i+str(1)然后 python 解釋器會接受它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM