我正在使用Selenium 2.20 WebDriver使用C#创建和管理firefox浏览器.要访问页面,我使用以下代码,在访问URL之前设置驱动程序超时:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); // Set implicit wait timeouts to 5 secs
driver.Manage().Timeouts().SetScriptTimeout(new TimeSpan(0, 0, 0, 5)); // Set script timeouts to 5 secs
driver.Navigate().GoToUrl(myUrl); // Goto page url
Run Code Online (Sandbox Code Playgroud)
问题是有时页面需要永远加载,并且看起来使用selenium WebDriver加载页面的默认超时是30秒,这太长了.我不相信我设置的超时适用于使用GoToUrl()方法加载页面.
所以我试图弄清楚如何设置页面加载的超时,但是,我找不到任何实际工作的属性或方法.当我单击一个元素时,默认的30秒超时似乎也适用.
有没有办法将页面加载超时设置为特定值,以便当我调用GoToUrl()方法时,它只会等待我指定的时间才能继续?
我遇到了问题,我想等待10秒,因为我希望我的应用程序在10秒后启动下面的代码,但不会阻止该人点击应用程序中的任何其他内容(不调用Thread.sleep();
).
try {
Log.v("msg", "WAIT CheckFrequencyRun");
Thread.sleep(10000); // giving time to connect to wifi
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//if no network
if(wifiManager.getConnectionInfo().getNetworkId()==-1){
//stop wifi
wifiManager.setWifiEnabled(false);
Log.v("msg", "no connection");
handler.postDelayed(this, checkInterval);
}
//else connection
else{
Log.v("msg", "connection");
onDestroy();
}
Run Code Online (Sandbox Code Playgroud) 我想向服务器发送请求并处理返回的值:
private static string Send(int id)
{
Task<HttpResponseMessage> responseTask = client.GetAsync("aaaaa");
string result = string.Empty;
responseTask.ContinueWith(x => result = Print(x));
responseTask.Wait(); // it doesn't wait for the completion of the response task
return result;
}
private static string Print(Task<HttpResponseMessage> httpTask)
{
Task<string> task = httpTask.Result.Content.ReadAsStringAsync();
string result = string.Empty;
task.ContinueWith(t =>
{
Console.WriteLine("Result: " + t.Result);
result = t.Result;
});
task.Wait(); // it does wait
return result;
}
Run Code Online (Sandbox Code Playgroud)
我使用Task
得当吗?我不这么认为,因为该Send()
方法string.Empty
每次都Print
返回,同时返回正确的值.
我究竟做错了什么?如何从服务器获得正确的结果?
对于子进程,可以使用wait()
和waitpid()
函数暂停当前进程的执行,直到子进程退出.但是此功能不能用于非子进程.
还有其他功能,可以等待退出任何进程吗?
有这个等待声明:
public final native void wait(long timeout) throws InterruptedException;
Run Code Online (Sandbox Code Playgroud)
它可以通过InterruptedException或超时退出,或者因为在另一个线程中调用Notify/NotifyAll方法,Exception很容易捕获但是...
有什么方法可以知道退出原因是超时还是通知?
编辑:
这是一种可行的方法,(虽然我不喜欢)
long tBefore=System.currentTimeMillis();
wait(TIMEOUT);
if ((System.currentTimeMillis() - tBefore) > TIMEOUT)
{
//timeout
}
Run Code Online (Sandbox Code Playgroud) 非常具体的问题(我希望):以下三个代码之间有什么区别?
(我希望它只是第一个不等待子进程完成,而第二个和第三个进行.但我需要确定这是唯一的区别......)
我也欢迎其他评论/建议(虽然我已经很清楚shell=True
危险和跨平台限制)
请注意,我已经阅读过Python子进程交互,为什么我的进程可以使用Popen.communicate,但不能使用Popen.stdout.read()?并且我不希望/之后需要与程序交互.
另请注意,我已经阅读了Python Popen.communicate()内存限制的替代品?但是我没有真正得到它......
最后,请注意我知道当一个缓冲区使用一种方法填充一个输出时存在死锁的风险,但我在互联网上寻找清晰的解释时迷路了......
第一个代码:
from subprocess import Popen, PIPE
def exe_f(command='ls -l', shell=True):
"""Function to execute a command and return stuff"""
process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)
stdout = process.stdout.read()
stderr = process.stderr.read()
return process, stderr, stdout
Run Code Online (Sandbox Code Playgroud)
第二个代码:
from subprocess import Popen, PIPE
from subprocess import communicate
def exe_f(command='ls -l', shell=True):
"""Function to execute a command and return stuff"""
process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)
(stdout, stderr) …
Run Code Online (Sandbox Code Playgroud) 今天,我有一个采访上,我问候选人很平常和基本的问题有关的区别Thread.sleep()
和Object.wait()
.我希望他回答类似这样的事情,但他说这些方法基本上都是一样的,很可能Thread.sleep
是Object.wait()
在里面使用,但sleep
本身并不需要外部锁定.这不是一个正确的答案,因为在JDK 1.6中,此方法具有以下签名.
public static native void sleep(long millis) throws InterruptedException;
Run Code Online (Sandbox Code Playgroud)
但我的第二个想法是,这不是那么荒谬.可以使用定时等待来实现相同的效果.看一下下面的代码片段:
public class Thread implements Runnable {
private final Object sleepLock = new Object();
// other implementation details are skipped
public static void sleep(long millis) throws InterruptedException {
synchronized (getCurrentThread().sleepLock){
getCurrentThread().sleepLock.wait(millis);
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,sleepLock
是一个特别用于sleep
方法内同步块的对象.我假设Sun/Oracle工程师知道Occam的剃须刀,因此sleep
本地实现是故意的,所以我的问题是为什么它使用本机调用.
我想出的唯一想法是假设有人可能会找到有用的调用Thread.sleep(0)
.根据这篇文章对调度程序管理有意义:
这具有清除当前线程的量子并将其置于队列末尾以获得其优先级的特殊效果.换句话说,所有具有相同优先级的可运行线程(以及具有更高优先级的线程)将有机会在下一个给定CPU时间产生的线程之前运行.
所以一个synchronized
块会带来不必要的开销.
您是否知道在Thread.sleep()
实施中不使用定时等待的任何其他原因?
在Cocoa中等待特定时间的方式是否比我在下面提出的更直接?
- (void) buttonPressed {
[self makeSomeChanges];
// give the user some visual feedback and wait a bit so he can see it
[self displayThoseChangesToTheUser];
[self performSelector:@selector(buttonPressedPart2:) withObject:nil afterDelay:0.35];
}
- (void) buttonPressedPart2: (id)unused {
[self automaticallyReturnToPreviousView];
}
Run Code Online (Sandbox Code Playgroud)
为了清楚起见,这段代码没有任何功能问题 - 我唯一的牛肉是风格的.在我的情况下,流程很简单,它可以工作,但尝试封装它或抛出一些条件,事情可能变得丑陋.有点唠叨我,我无法找到一种方法等待,然后回到像这个(虚构)示例的代码中的相同点:
- (void) buttonPressed {
[self doStuff];
[UIMagicUnicorn waitForDuration:0.35];
[self doStuffAfterWaiting];
}
Run Code Online (Sandbox Code Playgroud) 我需要使用fork()和wait()函数来完成赋值.我们正在对非确定性行为进行建模,如果存在多个可能的转换,则需要fork()程序.
为了尝试解决fork和wait如何工作,我刚刚制作了一个简单的程序.我想现在我理解调用是如何工作的,并且如果程序只分支一次会很好,因为父进程可以使用单个子进程的退出状态来确定子进程是否达到接受状态.
从下面的代码中可以看出,我希望能够处理必须有多个子进程的情况.我的问题是你似乎只能使用_exit函数设置状态一次.因此,在我的示例中,父进程测试的退出状态显示第一个子进程在其退出状态时发出0,但没有关于第二个子进程的信息.
我试着简单地不是_exit() - 拒绝,但那个子进程会继续,实际上似乎有两个父进程.
对于华夫饼干感到抱歉,但如果有人能告诉我父进程如何获取有关多个子进程的状态信息,我将不胜感激,或者我很高兴父进程只能注意到来自子进程的接受状态,但在这种情况下,我将成功地需要退出具有拒绝状态的子进程.
我的测试代码如下:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(void) {
pid_t child_pid, wpid, pid;
int status = 0;
int i;
int a[3] = {1, 2, 1};
for(i = 1; i < 3; i++) {
printf("i = %d\n", i);
pid = getpid();
printf("pid after i = %d\n", pid);
if((child_pid = fork()) == 0) {
printf("In child process\n");
pid = getpid();
printf("pid in child process is %d\n", pid);
/* Is a …
Run Code Online (Sandbox Code Playgroud) 在kotlin有等待功能吗?(我不是指计时器计划,但实际上暂停执行).我已经读过你可以使用了Thread.sleep()
.但是,它对我不起作用,因为无法找到该功能.