我想為測驗專案組織我的 url,所以我嘗試創建一個單獨的類或字典來存盤 url,但是每當我嘗試呼叫“driver.get(pages.url)”方法時,都會出錯。盡管如果您直接從變數或字串傳遞 url,一切正常。我不明白問題出在哪里。有人可以幫忙嗎?編碼:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
class Pages:
basic_url:str = 'google.com'
translate:str = 'translate.' basic_url
maps:str = 'www.' basic_url '/maps'
pages = Pages()
print(pages.translate)
print(type(pages.translate))
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(executable_path=ChromeDriverManager().install()),options=chrome_options)
wait = WebDriverWait(driver, 60)
driver.maximize_window()
driver.get(pages.translate)
錯誤:
'Traceback (most recent call last):
File "E:\Projects\Python\portal_UI_testing_automation\ptn.py", line 23, in <module>
driver.get(pages.translate)
File "E:\portal_UI_testing_automation\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in get
self.execute(Command.GET, {'url': url})
File "E:\portal_UI_testing_automation\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in execute
self.error_handler.check_response(response)
File "E:\portal_UI_testing_automation\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
(Session info: chrome=106.0.5249.119)'
uj5u.com熱心網友回復:
在將其傳遞給 driver.get 之前列印您的 url 以確保其有效。正如一些評論建議嘗試添加 http/https。
uj5u.com熱心網友回復:
我認為您在 URL 中缺少 https://
這是正確的答案。感謝 Sighil 和其他人。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/520172.html
標籤:Python硒网址得到
