1. 启动浏览器 # 导入selenium库fromseleniumimportwebdriver# 启动Chrome浏览器,创建一个浏览器对象browser=webdriver.Chrome() 1. 2. 3. 4. 5. 2. 打开指定网页 # 打开指定网页browser.get(" 1. 2. 3. 获取当前页面URL # 获取当前页面URLcurrent_url=browser.current
fromseleniumimportwebdriverimporttime# 设置Chrome的选项options=webdriver.ChromeOptions()options.add_argument('--headless')# 无头模式,不显示浏览器窗口# 创建WebDriver对象driver=webdriver.Chrome(options=options)# 打开指定网址driver.get('# 等待页面加载完成time.sleep(2)# 获取当前页面的URLcurrent_url=driver....
current_url属性返回一个字符串,其中包含当前浏览页面的URL。 以下是获取当前URL的示例代码: fromseleniumimportwebdriver# 创建Chrome浏览器的WebDriver对象driver=webdriver.Chrome()# 打开百度首页driver.get("https://www.baidu.com")# 获取当前URLcurrent_url=driver.current_url# 打印当前URLprint("当前URL:",curr...
导入所需的库和模块:from selenium import webdriver 创建一个浏览器实例:driver = webdriver.Chrome() # 使用Chrome浏览器,需要下载对应的ChromeDriver并配置环境变量 打开一个网页:driver.get("https://www.example.com") # 替换为你要访问的网页URL 获取当前浏览器的URL:current_url = driver.current_url 保存...
在Python中使用Selenium获取当前URL是一个常见的自动化测试和数据抓取任务。下面我将详细解释如何使用Selenium获取当前页面的URL,并附上示例代码。 1. 导入Selenium库 首先,你需要确保已经安装了Selenium库。如果还没有安装,可以使用以下命令进行安装: bash pip install selenium 然后,在你的Python脚本中导入Selenium库:...
from seleniumimportwebdriver from webdriver_manager.chromeimportChromeDriverManager driver=webdriver.Chrome(ChromeDriverManager().install())driver.get("https://www.baidu.com/")# 设置指定大小窗口 driver.set_window_size(1980,1080)# 最大化 driver.maximize_window()# 获取当前URLcurrentUrl=driver.current_...
currentUrl=driver.current_url print('当前地址:{0}'.format(currentUrl)) 3、获取页面源代码 pagesourse=driver.page_source print('获取页面源代码:{0}'.format(pagesourse)) 4、获取当前标题 title=driver.title print('当前页面标题:{0}'.format(title)) 5、前进/后退操作 driver.get("https://ww...
Python Selenium 常用方法总结 1.获取当前页面的Url 方法:current_url 实例:driver.current_url 2.获取元素坐标 方法:location 解释:首先查找到你要获取元素的,然后调用location方法 实例:driver.find_element_by_xpath("xpath").location 3.表单的提交 方法:submit...
2.获取页面URL current_url:获取当前页面的URL from seleniumimportwebdriverimporttime browser=webdriver.Chrome() browser.get('https://www.baidu.com')#打印网页标题print(browser.current_url) #输出内容:https://www.baidu.com/ 3.获取浏览器版本号 ...
方法:get_window_position() 实例:driver.get_window_position() 获取当前窗口的长和宽 方法:get_window_size() 实例:driver.get_window_size() 鼠标事件 引入ActionChains类:from selenium.webdriver.common.action_chains import ActionChains 右击 方法:context_click() ...