使用 Python driver.current_url 语句获取 Selenium Automation 的完整 UR



我正在尝试从chrome的地址栏中获取完整的URL。我正在使用此代码。

import re
from selenium import webdriver
driver = webdriver.Chrome(r'C:UsersXYZDesktopABCchromedriver_win32chromedriver.exe')  # Optional argument, if not specified will search path.
driver.get('https://www.google.com/maps');
#time.sleep(1) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('India Gate')
driver.find_element_by_css_selector("button[id='searchbox-searchbutton']").click()
currentURL = driver.current_url
print(currentURL)
time.sleep(5)
driver.quit()

而不是在地址栏中获得完整的URL,我只会得到https://google.com/maps

我需要这样的网址https://www.google.com/maps/place/India+Gate/@28.6337379,77.2060128,15z/data=!4m5!3m4!1s0x390ce2daa9eb4d0b:0x717971125923e5d!8m2!3d28.612912!4d77.2295097

我错过了什么?请帮忙。这是我第一次使用硒。

要提取 url,您需要诱导WebDriverWait等待url_contains(),您可以使用以下解决方案:

  • 代码块:

    import re
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    driver = webdriver.Chrome(r'C:UsersXYZDesktopABCchromedriver_win32chromedriver.exe')  # Optional argument, if not specified will search path.
    driver.get('https://www.google.com/maps');
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("India Gate")
    driver.find_element_by_css_selector("button[id='searchbox-searchbutton']").click()
    WebDriverWait(driver, 20).until(EC.url_contains("Gate"))
    print(driver.current_url)
    driver.quit()
    
  • 控制台输出:

    https://www.google.com/maps/place/India+Gate/@28.6129167,77.227321,17z/data=!3m1!4b1!4m5!3m4!1s0x390ce2daa9eb4d0b:0x717971125923e5d!8m2!3d28.612912!4d77.2295097
    

更新

作为替代方法,您可以按如下方式使用url_contains("data=")而不是url_contains("Gate")

WebDriverWait(driver, 10).until(EC.url_contains("data="))

DebanjanB 提供的相同代码块在我增加等待时间时起作用。

import re
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(r'C:UsersXYZDesktopABCchromedriver_win32chromedriver.exe')  # Optional argument, if not specified will search path.
driver.get('https://www.google.com/maps');
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("India Gate")
driver.find_element_by_css_selector("button[id='searchbox-searchbutton']").click()
WebDriverWait(driver, 30).until(EC.url_contains("Gate"))
print(driver.current_url)
driver.quit()

这可能是因为您的互联网连接速度慢或其他因素。

根据您对我评论的反馈,我建议您使用以下代码片段:

import re
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(r'C:UsersXYZDesktopABCchromedriver_win32chromedriver.exe')  # Optional argument, if not specified will search path.
driver.get('https://www.google.com/maps');
#time.sleep(1) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('India Gate')
driver.find_element_by_css_selector("button[id='searchbox-searchbutton']").click()
WebDriverWait(driver, 10).until(EC.url_contains("https://www.google.com/maps/place/")) # here you are waiting until url will match your output pattern
currentURL = driver.current_url
print(currentURL)
time.sleep(5)
driver.quit()

PS尽可能避免硬编码暂停

最新更新