尝试使用selenium Python循环搜索查询时,在find_element_by_partial_link_text()中获取错误

时间:2017-06-16 18:38:06

标签: python selenium web-scraping beautifulsoup phantomjs

我要做的是为.csv创建搜索查询和解析数据的循环。但是代码在此行driver.find_element_by_partial_link_text(query).click()上出错,而​​Selenium停止工作。无法弄清楚发生了什么。

from selenium import webdriver
from bs4 import BeautifulSoup 
import html5lib
import lxml.html
import csv 
import time

driver = webdriver.Chrome()
driver.get("url"


with open('your_csv.csv','r') as csvfile:
    with open('log.csv','wb') as csv_out:
        writer=csv.writer(csv_out)
        spamreader = csv.reader(csvfile)
        for query in spamreader:            
            username = driver.find_element_by_name("p_name")
            username.send_keys(query)
            driver.find_element_by_xpath("html/body/form/table[6]/tbody/tr/td[2]/input").click() #this will take you on new link
            time.sleep(4)
            driver.find_element_by_partial_link_text(query).click()

            html = driver.page_source
            soup = BeautifulSoup(html, 'html.parser')
            words = soup.find_all("td")
            ralph =  soup.get_text().encode('utf-8')

            a = list()
            for jav in words:
                    a.append((jav.text).encode('utf-8'))

            with open('data.csv', 'w') as csvfile:
                spamwriter = csv.writer(csvfile,delimiter=',')
                spamwriter.writerow(a)

这是错误:

Traceback (most recent call last):
  File "ninja.py", line 21, in <module>
    driver.find_element_by_partial_link_text(query).click()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 361, in find_element_by_partial_link_text
    return self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 791, in find_element
    'value': value})['value']
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: 'value' must be a string
  (Session info: chrome=58.0.3029.110)
  (Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.5 x86_64)

2 个答案:

答案 0 :(得分:1)

我发现csv.reader()返回每一行,作为字符串列表。方括号。例如,如果CSV文件中的行包含。 “贾维斯”。它将返回[“jarvis”],这完全没有语法。这就是我所做的。只是条形方括号......

with open('your_csv.csv','r') as csvfile:
    with open('log.csv','wb') as csv_out:
        writer=csv.writer(csv_out)
        spamreader = csv.reader(csvfile)
        for query in spamreader:            
            username = driver.find_element_by_name("p_name")
            username.send_keys(str(query).strip('[').strip(']')

...

答案 1 :(得分:0)

问题在于您尝试使用find_element_by_link_text()方法中的列表。

你看,

    spamreader = csv.reader(csvfile)

您使用的reader()方法返回读取的每一行,作为字符串列表。 Here是详细阐述它的Python文档。

现在,您将传递此字符串列表,该列表在query中返回到find_element_by_link_text()方法,该方法仅将字符串作为参数 - 示例显示为here

您需要使用strip()方法去除字符串,然后使用第一个字符串作为find_element方法中的参数。