javascript - 将用户导航到 URL 并模拟用户按下向下箭头或单击按钮 3 次
问题描述
我在 wordpress 中使用了一个不包含锚点的滑块,以允许我们链接到特定部分。
是否可以使用 java 或 selenium 将用户导航到 URL 并模拟用户按下向下箭头或单击按钮 3 次。
我们可以锚定到文本块,但是分屏滑块的功能确实可以正确加载页面。
我们正在本地开发,但这里是主题示例。我正在尝试链接到幻灯片 3。http://tahoe.edge-themes.com/split-screen-slider/
后端功能不是我的强项。谢谢你。
解决方案
我只能看到一种使用 JavaScript 执行此操作的方法,将以下内容注入页面,在需要时进行更改:
// Get a reference to the button element needing to be clicked
var button = document.querySelector("button");
// Use the following function:
navigateToPage(button, "page");
// Link to page with query string of `?page=[n]` (see URL),
// then click the specified element n times
function navigateToPage(button, queryParamName) {
var times = parseInt(getQueryVariable(queryParamName), 10);
for (var i = 0; i < times; i++) {
button.click();
}
}
// Taken from: https://stackoverflow.com/questions/2090551/parse-query-string-in-javascript
// ----
// If you don't need to support IE 11, see https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/get
// for a more native solution to fetch query params.
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
return variable;
}
有关实时示例,请参见以下 Codesandbox:
https://codesandbox.io/s/ox0k1y16k6
根据 URL 中的查询参数观察按钮被点击的次数:
推荐阅读
- python - Windows python用户的quickfix库分步成功安装
- php - 自动嵌套循环
- php - 从 WordPress DB 检查元值不适用于页面
- vue.js - 在 Vuejs 中更改 v-model 输入值时动态数据不会更新
- c# - 当我使用 invokemember () Webbrowser 按下按钮时出现错误
- ios - UINavigationView 弹出到根视图控制器问题
- php - 从 cmd 运行时,来自 WAMP 服务器的 PHP 没有看到 .htaccess 文件
- google-apps-script - 类表方法 getActiveCell、getCurrentCell、getActiveRange 无法按预期工作
- ejabberd - 如何使用 Smack Client 在 Ejabberd 中注册带内用户,特别是虚拟主机?
- javascript - 将两个代码合二为一 - 检查 url 是否存在/登录