【发布时间】:2018-09-09 19:09:14
【问题描述】:
我想在运行 Selenium 时获取当前 url。 我查看了这个 stackoverflow 页面:How do I get current URL in Selenium Webdriver 2 Python? 并尝试了发布的内容,但它不起作用。我在下面附上我的代码:
from selenium import webdriver
#launch firefox
driver = webdriver.Firefox()
url1='https://poshmark.com/search?'
# search in a window a window
driver.get(url1)
xpath='//input[@id="user-search-box"]'
searchBox=driver.find_element_by_xpath(xpath)
brand="freepeople"
style="top"
searchBox.send_keys(' '.join([brand,"sequin",style]))
from selenium.webdriver.common.keys import Keys
#EQUIValent of hitting enter key
searchBox.send_keys(Keys.ENTER)
print(driver.current_url)
我的代码打印https://poshmark.com/search?但它应该打印:https://poshmark.com/search?query=freepeople+sequin+top&type=listings&department=Women,因为这就是 selenium 的用途。
【问题讨论】:
-
问题是您的
searchBox.send_keys(Keys.ENTER)和print(driver.current_url)之间没有延迟。应该有一些时间延迟,以便语句可以选择 url 更改。如果您的代码在 url 实际更改之前触发,它只会为您提供旧 url。解决方法是添加time.sleep(1)以等待 1 秒。 -
呵呵,我想知道是不是这样
-
这就是问题所在!谢谢! :)
标签: python-3.x selenium