Ad

How Can I Iterate Through An Excel Sheet To Perform A Search On A Webpage Python Selenium

- 1 answer

I want to iterate through a list of companies to search them one by one and save the 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 

I get the following error and I'm not able to sort it out, before even being able to scrape the 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 
Ad

Answer

The for loop assignes the strings found in df['company_name'] to it's variable i. At the end of the loop you add 1 to this string, which is not allowed, as the python interpreter can not implicitly cast an int to a string.

I have the feeling, that you are trying to use the i=i+1 as a loop counter variable, but that is not needed in a for-each-loop (i.e. for i in foo). Just delete that i=i+1. The loop will still run as expected.

However, if you really want to add a one to the string stored in i, you have to write it like this: i=i+str(1) Then the python interpreter will accept it.

Ad
source: stackoverflow.com
Ad