Last active
April 2, 2025 21:17
-
-
Save kevinoid/29510f6544d7201f4e2c25e8a871a61a to your computer and use it in GitHub Desktop.
Reproduction for Selenium WebDriver POST navigation issues on Chrome 129+ and Edge 134+
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cloudflare Worker hosted at https://post-test.kevinoid.workers.dev/ | |
function escapeXml(str) { | |
return str.replace(/[<>&'"]/g, function (c) { | |
switch (c) { | |
case '<': return '<'; | |
case '>': return '>'; | |
case '&': return '&'; | |
case '\'': return '''; | |
case '"': return '"'; | |
} | |
}); | |
} | |
export default { | |
async fetch(request, env, ctx) { | |
let formData; | |
try { | |
formData = await request.formData(); | |
} catch (err) { | |
console.error(err); | |
} | |
return new Response( | |
`<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<title>POST Test Page</title> | |
</head> | |
<body> | |
<h1>POST Test Page</h1> | |
<p>POST data: <span id="post_data">${escapeXml(formData?.get("data") ?? '')}</span></p> | |
<form method="post"> | |
<label for="data">Data:</label> | |
<input id="data" name="data" /><br /> | |
<input type="submit" value="Submit" /> | |
</form> | |
</body> | |
</html>`, | |
{ | |
headers: { | |
"content-type": "text/html", | |
}, | |
}); | |
}, | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Builder, Browser, By } from 'selenium-webdriver'; | |
const driver = await new Builder().forBrowser(Browser.CHROME, 129).build(); | |
// const driver = await new Builder().forBrowser(Browser.CHROME, 134).build(); | |
try { | |
await driver.get('https://post-test.kevinoid.workers.dev/'); | |
for (let attempt = 1; ; attempt += 1) { | |
// Subnmit HTML form with attempt number as data | |
const sentData = `${attempt}`; | |
await driver.findElement(By.id('data')).sendKeys(sentData); | |
await driver.findElement(By.css('input[type=submit]')).click(); | |
// Confirm that navigation has completed by checking that POST data in page matches the request. | |
// Note: StaleElementReferenceError often occurs due to ongoing navigation. | |
const pageData = await driver.findElement(By.id('post_data')).getText(); | |
if (sentData !== pageData) { | |
throw new Error( | |
`Received data did not match sent (${pageData} != ${sentData}) on ${ | |
attempt} attempt.`, | |
); | |
} | |
} | |
} finally { | |
await driver.quit(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "@kevinoid/selenium-post-test", | |
"version": "1.0.0", | |
"type": "module", | |
"dependencies": { | |
"selenium-webdriver": "^4.30.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenQA.Selenium; | |
using OpenQA.Selenium.Chrome; | |
using OpenQA.Selenium.Edge; | |
using var driver = new ChromeDriver(new ChromeOptions() { BrowserVersion = "129" }); | |
// using var driver = new EdgeDriver(new EdgeOptions() { BrowserVersion = "134" }); | |
driver.Url = "https://post-test.kevinoid.workers.dev/"; | |
for (var attempt = 1; ; attempt++) | |
{ | |
// Subnmit HTML form with attempt number as data | |
var sentData = attempt.ToString(); | |
driver.FindElement(By.Id("data")).SendKeys(sentData); | |
driver.FindElement(By.CssSelector("input[type=submit]")).Click(); | |
// Confirm that navigation has completed by checking that POST data in page matches the request. | |
// Note: StaleElementException often occurs due to ongoing navigation. | |
var pageData = driver.FindElement(By.Id("post_data")).Text; | |
if (!string.Equals(sentData, pageData)) | |
{ | |
Console.WriteLine($"Received data did not match sent ({pageData} != {sentData}) on {attempt} attempt."); | |
Environment.Exit(1); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net9.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Selenium.WebDriver" Version="4.30.0" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment