簡體   English   中英

selenium.common.exceptions.InvalidArgumentException:消息:在遍歷 url 列表並作為參數傳遞給 get() 時參數無效

[英]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.

相關問題 selenium.common.exceptions.InvalidArgumentException:消息:使用 Selenium Webdriver 通過 Python 調用 get() 時參數無效錯誤 selenium.common.exceptions.InvalidArgumentException:消息:無效參數錯誤調用 get() 與使用 Selenium Python 從文本文件讀取的 url selenium.common.exceptions.InvalidArgumentException:消息:無效參數:使用 selenium webdriver 添加 cookie 時缺少“cookie” Python selenium 用於撰寫評論谷歌地圖(selenium.common.exceptions.InvalidArgumentException:消息:無效參數:無效定位器) selenium.common.exceptions.InvalidArgumentException:消息:無效參數:使用 Selenium 上傳文件時找不到文件錯誤 Python selenium.common.exceptions.InvalidArgumentException:消息:無效參數:在 Selenium 中切換幀時缺少“元素”錯誤 URL 必須是字符串 selenium - selenium.common.exceptions.InvalidArgumentException:消息:無效參數:&#39;url&#39; 必須是字符串 Selenium By.CSS_SELECTOR selenium.common.exceptions.InvalidArgumentException:消息:參數無效 已解決:python:selenium.common.exceptions.InvalidArgumentException:消息:無效參數:無效定位器 selenium.common.exceptions.InvalidArgumentException:消息:無效參數:“使用”必須是使用等待和預期條件的字符串
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM