我正在使用 Playwright 和 JavaScript 测试 UI。我的代码找到一个输入元素,有时可以是下拉菜单,有时是文本,有时是日期。为了解决这个问题,我分两步输入值。首先,我填充文本,然后单击 Tab 键来调用格式化元素中的值的 JavaScript。
await page.fill("#myID", inputText);
await page.keyboard.press('Tab'); // this line trigger the JS
// continue to the next element
Run Code Online (Sandbox Code Playgroud)
问题是,它没有等待 JavaScript 完成。我怎样才能等待 JS 完成然后代码继续。
我有这个页面,其中有一个文本框,每个文本框都有一个保存按钮.我需要单击"保存"按钮,以便将值保存在文本框中.它正在手动工作并使用硒.但是当运行Selenium WebDriver时,它不会保存文本框值.但是没有抛出错误异常.输入,点击正在运行.savetextvalue()不会被触发.有类似的问题Selenium click事件不会触发angularjs事件
<pp-save-control fn-save-text="saveText();" btn-class="btn btn-default btn-mtl" button-id="btnkbaemailauthsub" place-holder-text="" input-class="tb-mtl" input-id="txtkbaemailauthsub" config-name="40" title-text="KBA email authentication subject" outer-container-class="div-mtl-header" class="ng-isolate-scope"><div class="div-mtl-header">
<span class="label-mtl ng-binding">KBA email authentication subject</span><img ng-hide="(isHelpHidden != null && isHelpHidden != 'true') ? false : true" class="help-mtl ng-hide" src="/Images/help.png">
<div class="div-mtl-tb-holder">
<input type="text" placeholder="" class="tb-mtl" name="txtkbaemailauthsub" id="txtkbaemailauthsub">
<button ng-click="saveTextValue();" ng-hide="false" class="btn btn-default btn-mtl btn-mtl-alignment" name="btnkbaemailauthsub" id="btnkbaemailauthsub" type="button">save</button>
</div>
</div>
</pp-save-control>
Run Code Online (Sandbox Code Playgroud)
有多个文本框和关联的保存按钮.根据'config-value'(您可以在顶部看到),值将被保存.
我们使用Selenium WebDriver自动化基于UI的测试.我们的挑战之一是检测页面何时真正完成加载,而Angular 1在这方面也是一个挑战.我们最终执行这段代码专门检测Angular 1是否完成:
if(typeof window.angular !== \"undefined\")
{
var injector = window.angular.element(\"*[ng-app]\").eq(0).injector();
if(injector)
{
var $rootScope = injector.get(\"$rootScope\");
var $http = injector.get(\"$http\");
if($rootScope.$$phase === \"$apply\" || $rootScope.$$phase === \"$digest\" || $http.pendingRequests.length !== 0)
{
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我们最近测试的应用程序切换到使用Angular 2.上面的代码片段不等待Angular 2完成.有什么建议?
所以我遇到了与下面这个HTML交互的问题.我无法打开弹出窗口并使用不同的Selenium Webdriver命令关闭它.虽然我在这里寻找一个特定的解决方案,但任何有关处理Angular JS的一般提示也将受到赞赏.我相信问题的根本原因是我不知道使用Selenium Webdriver自动化Angular的好方法.
我正在使用C#,但我会从任何编程语言中获取任何有用的信息,因为我总是可以改进解决方案.
我正在尝试与此弹出按钮进行交互,但未成功:uib-popover-template ="'/ app/student/assessment/directives/templates/shortTextPopupTemplate.html'"
<div class="line ttsBorder">“<span style="font-style:italic;">And be it further enacted</span><span style="display: inline;">, That in all that territory ceded</span><span short-text-popup="" accession-number="VH209006" class="ng-isolate-scope" ng-non-bindable=""><a uib-popover-template="'/app/student/assessment/directives/templates/shortTextPopupTemplate.html'" popover-is-open="ctrl.isVisible" popover-trigger="none" popover-placement="auto top" tabindex="0" title="Shows more information." ng-click="buttonOnClick($event)" ng-keydown="buttonOnKeydown($event)" ng-class="['stpu-button', {disabled: ctrl.isDisabled, active: ctrl.isVisible}]" class="ng-scope stpu-button" style="z-index: 3;"></a></span> by France to the United States . . . which lies north of thirty?six degrees and thirty minutes north latitude . . . slavery . . . shall be, and is hereby, forever prohibited.”</div>Run Code Online (Sandbox Code Playgroud)
这是我尝试失败的原因:
//attempt 1
var elements = Driver.FindElements(By.XPath("//*[@class='line ttsBorder']"));
//attempt 2 …Run Code Online (Sandbox Code Playgroud) 为什么当我添加time.sleep(2)时,我得到了我想要的输出但是如果我添加等待直到特定的xpath它会得到更少的结果?
输出time.sleep(2)(也需要):
Adelaide Utd
Tottenham
Dundee Fc
...
Run Code Online (Sandbox Code Playgroud)
数:145名
删除time.sleep
Adelaide Utd
Tottenham
Dundee Fc
...
Run Code Online (Sandbox Code Playgroud)
数:119个名字
我已经添加了:
clickMe = wait(driver, 13).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ("#page-container > div:nth-child(4) > div > div.ubet-sports-section-page > div > div:nth-child(2) > div > div > div:nth-child(1) > div > div > div.page-title-new > h1"))))
Run Code Online (Sandbox Code Playgroud)
因为这个元素出现在所有页面上.
似乎要少得多.我该如何解决这个问题?
脚本:
import csv
import os
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait …Run Code Online (Sandbox Code Playgroud) 我使用selenium将网站表格中的数据转化为分析.我必须扫描并获得约1000行的表格.
我知道该页面有Javascript,它会自动更新DOM.但是这个表太多了,所以当我扫描我的代码时总会抛出一个异常.
我试过这段代码:
Boolean breakIt = true;
List<IWebElement> result = new List<IWebElement>();
while (true)
{
breakIt = true;
try
{
IWebElement baseTable = browser.FindElementById("column2");
ReadOnlyCollection<IWebElement> rowsTable = baseTable.FindElements(By.XPath("id('oTable')/table/tbody/tr"));
foreach (IWebElement rows in rowsTable) {
if (rows.FindElements(By.XPath("td")).Count == 10)
result.Add(rows);
}
if (breakIt)
{
break;
}
}
catch (StaleElementReferenceException ser)
{
if (ser.Message.Contains("element is not attached"))
{
browser.Refresh();
browser.WaitForPageToLoad();
browser.GoToFrame(browser.FindElementByXPath("//*[@id='form1']/div[3]/iframe"));
breakIt = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
它会抛出一个例外:
陈旧元素引用:元素未附加到页面文档.
有什么想法解决我的问题吗?我认为使用多个Thread是最好的方法.
但我试过多次Thread它也返回异常.
得到后我想rowsTable.Count.除此/ 2.并创建两个线程运行这个?
selenium ×5
angularjs ×2
c# ×2
angular ×1
javascript ×1
playwright ×1
puppeteer ×1
python ×1
selenium-ide ×1
webdriver ×1