from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from time import sleep driver = webdriver.Chrome() driver.get("http://www.baidu.com") driver.find_element(By.ID,"kw").send_keys("selenium") driver.find_element(By.ID,"su").click() # 点击搜索结果当中的 selenium官网。 结果:打开一个新窗口。 loc = (By.XPATH,"//a[text()=\" automates browsers. That's it!\"]") wait = WebDriverWait(driver,20) wait.until(EC.visibility_of_element_located(loc)) driver.find_element(*loc).click() # 切换到新的窗口,然后在新窗口当中,点击 # 获取 sleep(1) win_hans = driver.window_handles # 列表。按照窗口出现的先后顺序添加 print("打开的窗口句柄们: ",win_hans) print("当前正在使用的窗口句柄:",driver.current_window_handle) # 切换到新窗口。进入了新的html driver.switch_to.window(win_hans[-1]) # 新的页面里,等待元素可见,查找元素,操作元素 loc = (By.XPATH,'//h3[text()="Selenium IDE"]/following-sibling::div//a') wait.until(EC.visibility_of_element_located(loc)) driver.find_element(*loc).click() sleep(10) driver.quit()
时间: 2023-12-06 15:43:18 浏览: 288
这段代码使用了 Selenium 库来实现自动化测试,具体实现了在百度搜索 selenium,然后点击结果中的 selenium 官网链接,进入新的窗口后点击该页面上的某个链接。其中用到了 WebDriver 的方法和一些等待条件的判断,在操作前进行了元素查找和定位。最后需要注意的是,测试完成后需要关闭 WebDriver。
相关问题
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from time import sleep driver = webdriver.Chrome() driver.get('http://www.baidu.com') # 隐式等待5秒 driver.implicitly_wait(5) loc = (By.XPATH, '//input[@id="kw"]') driver.find_element(*loc).send_keys('selenium') driver.find_element(By.XPATH, '//input[@id="su"]').click() wait = WebDriverWait(driver, 20) loc = (By.XPATH, '//b[text()="selenium"]/parent::span') wait.until(EC.visibility_of_element_located(loc)) driver.find_element(*loc).click() sleep(3) driver.quit()写下注释
这段代码使用了Selenium库来控制Chrome浏览器自动化测试。具体注释如下:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
# 初始化一个Chrome浏览器实例
driver = webdriver.Chrome()
# 打开百度首页
driver.get('http://www.baidu.com')
# 隐式等待5秒
driver.implicitly_wait(5)
# 定位搜索框并输入关键词'selenium'
loc = (By.XPATH, '//input[@id="kw"]')
driver.find_element(*loc).send_keys('selenium')
# 定位搜索按钮并点击
loc = (By.XPATH, '//input[@id="su"]')
driver.find_element(*loc).click()
# 显式等待20秒,直到页面上的'selenium'元素可见
wait = WebDriverWait(driver, 20)
loc = (By.XPATH, '//b[text()="selenium"]/parent::span')
wait.until(EC.visibility_of_element_located(loc))
# 点击'selenium'元素
driver.find_element(*loc).click()
# 停留3秒后关闭浏览器
sleep(3)
driver.quit()
```
这段代码的功能是:打开百度首页,输入关键词"selenium"进行搜索,然后在搜索结果页面中点击"百度百科"中的"selenium"词条,最后停留3秒后关闭浏览器。其中使用了隐式等待和显式等待来控制程序等待页面加载完成。
from selenium.webdriver.support.wait import WebDriverWait
`WebDriverWait`是Selenium中的一个类,用于等待特定条件出现后再进行下一步操作。在使用WebDriverWait时,我们需要指定等待的最长时间和等待条件,如果在等待时间内等待条件未满足,则会抛出一个TimeoutException异常。使用示例:
```
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "kw"))
)
element.send_keys("Selenium")
```
以上代码中,我们在打开百度页面后,使用WebDriverWait等待10秒钟,直到页面中id为“kw”的元素出现后,才进行输入操作。如果在10秒钟内元素未出现,则抛出TimeoutException异常。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)