This is the 20th day of my participation in the Genwen Challenge
preface
Python uses Selenium to automate testing in the browser. Here’s how.
To install selenium
PIP install -u selenium Note the code file name cannot be written as Selenium, otherwise it will be reported
ImportError: cannot import name 'webdriver' from partially initialized module 'selenium' (most likely due to a circular import)
Copy the code
Install the Chrome Driver
Need to install the driver to operate the browser, I put the D set the download address: chromedriver.chromium.org/ need to submit
Controls browser access to web pages
from selenium import webdriver Start the browser
driver = webdriver.Chrome('D:/chromedriver.exe') # Optional argument, if not specified will search path.
login_url = 'https://www.baidu.com'
driver.get(login_url)
Copy the code
Access elements on the page
You can just use find_element_by_xpath, and nothing else is as good as using methods to get elements after get
driver.implicitly_wait(20)
driver.find_element_by_xpath("//input[@id='kw']").send_keys("Temporary camp")
driver.find_element_by_xpath("//input[@id='su']").click()
Copy the code
To ensure that all the elements on the page have loaded successfully, wait 20 seconds
Access elements loaded lazily
Sometimes elements don’t come out all at once, such as a list element created after an Ajax request and a P element created after 10 seconds
<! doctype html> <html> <meta charset=utf-8>
<title>Race Condition Example</title>
<script>
setTimeout(function () {
var newElement = document.createElement("p");
newElement.textContent = "Hello from JavaScript!";
document.body.appendChild(newElement);
}, 10000);
</script>
</html>
Copy the code
An implicit wait
What I just introduced
driver.implicitly_wait(20)
Copy the code
Implicit waiting, which I don’t recommend because it might take longer.
Explicit waiting
The previous 20-second wait is not very elegant because the load time is probably less than 20 seconds. Here’s a way to query for such a later element
from selenium import webdriver Start the browser
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome('D:/chromedriver.exe') # Optional argument, if not specified will search path.
login_url = 'file:///C:/Users/wlzcool/Desktop/a.html'
driver.get(login_url)
# el = driver.find_element_by_xpath("//p")
el = WebDriverWait(driver, 20).until(lambda d: d.find_element_by_xpath("//p"))
print(el.text)
Copy the code
el = WebDriverWait(driver, 20).until(lambda d: D. pin_element_by_xpath (“//p”)) indicates that the code will wait up to 20 seconds until the p tag appears.
Pay attention to
Implicit wait and explicit wait should not be used together.
Get information on alert
<! doctype html> <html> <meta charset=utf-8>
<title>Race Condition Example</title>
<script>
setTimeout(function () {
var newElement = document.createElement("p");
newElement.textContent = "Hello from JavaScript!";
newElement.onclick=function(){alert("test alert text"); } document.body.appendChild(newElement); },10000);
</script>
</html>
Copy the code
The official written here is not very detailed, the from the selenium. Webdriver. Support the import expected_conditions this didn’t write it, Expected_conditions.alert_is_present () does not get up unless it finds the corresponding class
from selenium import webdriver Start the browser
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome('D:/chromedriver.exe') # Optional argument, if not specified will search path.
login_url = 'file:///C:/Users/wlzcool/Desktop/a.html'
driver.get(login_url)
# el = driver.find_element_by_xpath("//p")
el = WebDriverWait(driver, 20).until(lambda d: d.find_element_by_xpath("//p"))
el.click()
# Wait for the alert to be displayed and store it in a variable
alert = WebDriverWait(driver, 20).until(expected_conditions .alert_is_present())
# Store the alert text in a variable
text = alert.text
print(text)
Copy the code
Reference: stackoverflow.com/questions/1…
alert
Alert Click ok
alert.accept()
Copy the code
confirm
Confirm how to click the cancel button
alert.dismiss()
Copy the code
It’s not very useful in practice, and now it’s very rare to use an official alert.
conclusion
Life is short, I use Python, and with the power of Python, we can implement our automated testing requirements with just a few lines of code.