[英]selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() using Selenium Webdriver through Python
[英]selenium.common.exceptions.InvalidArgumentException: Message: invalid argument while iterating through a list of urls and passing as argument to get()
我正在抓取一個頁面以獲取 URL,然后使用它們抓取一堆信息。 我想避免一直復制和粘貼,但我找不到如何使 get() 與 object 一起工作。我的代碼的第一部分工作得很好,但是當我到達試圖獲取 url 的部分時我收到以下錯誤消息:
Traceback (most recent call last):
File "/Users/rcastong/Desktop/imgs/try-creating-object-url.py", line 61, in <module>
driver4.get(urlworks2)
File "/Users/rcastong/Library/Python/3.9/lib/python/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "/Users/rcastong/Library/Python/3.9/lib/python/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/rcastong/Library/Python/3.9/lib/python/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
(Session info: chrome=98.0.4758.109)
這是代碼的一部分
#this part works well
for number, item in enumerate(imgs2, 1):
# print('---', number, '---')
img_url = item.get_attribute("href")
if not img_url:
print("none")
else:
print('"'+img_url+'",')
# the error happens on driver4.get(urlworks2)
for i in range(0,30):
urlworks = img_url[i]
urlworks2 = urlworks.encode('ascii', 'ignore').decode('unicode_escape')
driver4 = webdriver.Chrome()
driver4.get(urlworks2)
def check_exists_by_xpath(xpath):
try:
WebDriverWait(driver3,55).until(EC.presence_of_all_elements_located((By.XPATH, xpath)))
except TimeoutException:
return False
return True
imgsrc2 = WebDriverWait(driver3,55).until(EC.presence_of_all_elements_located((By.XPATH, "//p[@data-testid='artistName']/ancestor::a[contains(@class,'ChildrenLink')]")))
for number, item in enumerate(imgsrc2, 1):
# print('---', number, '---')
artisturls = item.get_attribute("href")
if not artisturls:
print("none")
else:
print('"'+artisturls+'",')
這個錯誤信息...
Traceback (most recent call last):
.
driver4.get(urlworks2)
.
self.execute(Command.GET, {'url': url})
.
self.error_handler.check_response(response)
.
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
(Session info: chrome=98.0.4758.109)
...暗示作為參數傳遞給get()
的url
是一個無效參數。
在第一個for
循環中, item.get_attribute("href")
返回一個 url 字符串,並且img_url
在每次迭代時都會更新。 所以實際上img_url仍然是一個字符串,而不是你假設的 url 的列表。 因此,在第二個for
循環中,當您嘗試遍歷字符串的元素並將它們傳遞給get()
時,您會看到錯誤InvalidArgumentException: Message: invalid argument
。
例如下面的代碼行:
img_url = 'https://www.google.com/'
for i in range(0,5):
urlworks = img_url[i]
urlworks2 = urlworks.encode('ascii', 'ignore').decode('unicode_escape')
print(urlworks2)
印刷:
h
t
t
p
s
在全局 scope 中聲明一個空列表img_url
並繼續將hrefs附加到列表中,以便稍后迭代列表。
img_url = []
for number, item in enumerate(imgs2, 1):
img_url.append(item.get_attribute("href"))
您可以在以下位置找到一些相關的詳細討論:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.