使用 python 在 selenium 中检索更新的 url
Retrieve updated url in selenium using python
如何使用 selenium 和 Python 从 firefox 浏览器获取更新的 url?下面的代码是我正在尝试做的一个很好的工作示例。该脚本打开一个 url,在网页中查找搜索栏,粘贴特定产品,然后执行搜索。
我正在尝试在搜索完成后提取更新的 url,应该是 https://www.myntra.com/avene-unisex-thermal-spring-water-50-ml
,但我得到的是 https://www.myntra.com/
。我怎样才能得到所需的 url?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
firefoxOptions = webdriver.FirefoxOptions()
firefoxOptions.set_preference("dom.webnotifications.enabled", False)
driver = webdriver.Firefox(firefox_options = firefoxOptions)
driver.implicitly_wait(5)
# Maximize the browser window
driver.maximize_window()
# navigate to the home page
driver.get("https://www.myntra.com/")
# Locate the text field to update values
text_field = driver.find_element_by_class_name("desktop-searchBar")
# Clears any value already present in text field
text_field.clear()
# Updates the string in search bar
text_field.send_keys("Avene Unisex Thermal Spring Water 50 ml")
text_field.send_keys(Keys.ENTER)
new_page = driver.current_url
print(new_page)
driver.close()
您需要等待 text_field.send_keys(Keys.ENTER)
之后才能更新信息,
如果您不想等待,请尝试使用 click() 方法而不是 send_keys
看来你很接近。您需要为 url 引入 WebDriverWait 进行更改,您可以使用以下解决方案:
代码块:
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.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://www.myntra.com/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.desktop-searchBar"))).send_keys("Avene Unisex Thermal Spring Water 50 ml")
driver.find_element_by_css_selector("a.desktop-submit").click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h1.title-title")))
print(driver.current_url)
driver.quit()
控制台输出:
https://www.myntra.com/avene-unisex-thermal-spring-water-50-ml
如何使用 selenium 和 Python 从 firefox 浏览器获取更新的 url?下面的代码是我正在尝试做的一个很好的工作示例。该脚本打开一个 url,在网页中查找搜索栏,粘贴特定产品,然后执行搜索。
我正在尝试在搜索完成后提取更新的 url,应该是 https://www.myntra.com/avene-unisex-thermal-spring-water-50-ml
,但我得到的是 https://www.myntra.com/
。我怎样才能得到所需的 url?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
firefoxOptions = webdriver.FirefoxOptions()
firefoxOptions.set_preference("dom.webnotifications.enabled", False)
driver = webdriver.Firefox(firefox_options = firefoxOptions)
driver.implicitly_wait(5)
# Maximize the browser window
driver.maximize_window()
# navigate to the home page
driver.get("https://www.myntra.com/")
# Locate the text field to update values
text_field = driver.find_element_by_class_name("desktop-searchBar")
# Clears any value already present in text field
text_field.clear()
# Updates the string in search bar
text_field.send_keys("Avene Unisex Thermal Spring Water 50 ml")
text_field.send_keys(Keys.ENTER)
new_page = driver.current_url
print(new_page)
driver.close()
您需要等待 text_field.send_keys(Keys.ENTER)
之后才能更新信息,
如果您不想等待,请尝试使用 click() 方法而不是 send_keys
看来你很接近。您需要为 url 引入 WebDriverWait 进行更改,您可以使用以下解决方案:
代码块:
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.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe') driver.get("https://www.myntra.com/") WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.desktop-searchBar"))).send_keys("Avene Unisex Thermal Spring Water 50 ml") driver.find_element_by_css_selector("a.desktop-submit").click() WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h1.title-title"))) print(driver.current_url) driver.quit()
控制台输出:
https://www.myntra.com/avene-unisex-thermal-spring-water-50-ml