gpt4 book ai didi

python - 尝试使用 selenium Python 循环搜索查询时在 find_element_by_partial_link_text() 中出错

转载 作者:太空宇宙 更新时间:2023-11-04 05:05:37 25 4
gpt4 key购买 nike

我想做的是在 .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)

最佳答案

我发现 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(']')

...

关于python - 尝试使用 selenium Python 循环搜索查询时在 find_element_by_partial_link_text() 中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44595934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com