Open annidy opened 12 months ago
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v108.network.Network;
public class CodekruTest {
public static void main(String[] args) {
// pass the path of the chromedriver location in the second argument
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
DevTools devTools = ((ChromeDriver) driver).getDevTools();
devTools.createSession();
devTools.send(Network.enable(Optional.of(1000000), Optional.empty(), Optional.empty()));
devTools.addListener(Network.requestWillBeSent(), request -> {
System.out.println("Request Method : " + request.getRequest().getMethod());
System.out.println("Request URL : " + request.getRequest().getUrl());
System.out.println("Request headers: " + request.getRequest().getHeaders().toString());
System.out.println("Request body: " + request.getRequest().getPostData().toString());
});
driver.get("https://www.makemytrip.com/");
}
}
https://www.codekru.com/selenium/how-to-get-network-call-requests-in-selenium
https://stackoverflow.com/questions/72912626/selenium-4-chrome-devtools-using-python-fetch-fail-request-not-failing-the-reque
2. Use logs
import json from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.CHROME
caps['goog:loggingPrefs'] = {'performance': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=caps)
driver.get('https://stackoverflow.com/questions/52633697/selenium-python-how-to-capture-network-traffics-response')
def process_browser_log_entry(entry): response = json.loads(entry['message'])['message'] return response
browser_log = driver.get_log('performance') events = [process_browser_log_entry(entry) for entry in browser_log] events = [event for event in events if 'Network.response' in event['method']]
https://stackoverflow.com/questions/52633697/selenium-python-how-to-capture-network-traffics-response
https://gist.github.com/lorey/079c5e178c9c9d3c30ad87df7f70491d
3. Use seleniumwire
options = ChromeOptions() options.add_argument(r'''--user-data-dir=C:\Users\Admin\AppData\Local\Google\Chrome\User Data\'''') driver = webdriver.Chrome(options)
driver.get("xxx")
find all and filter
regex_classname = re.compile("index\w+") [e for e in driver.find_elements(By.XPATH, ".//*") if regex_class_name.match(str(e.get_attribute("class")))]
"//input[contains(@name,'sel')]" "//input[starts-with (@name,'Tut')]" "//input[ends-with (@name,'nium')]"
browser.execute_script( ''' var kw = document.getElementById('kw'); var su = document.getElementById('su'); kw.value = 'Selenium'; su.click(); ''' )