父css选择器的XPath(始终可见)如下:
/html/body/section/div/div[2]
大约5秒后(在一些javascript之后),其中一个孩子将可见。我想通过它的xpath选择这个可见的孩子。
/html/body/section/div/div[2]/div[1]
/html/body/section/div/div[2]/div[2]
/html/body/section/div/div[2]/div[3]
/html/body/section/div/div[2]/div[4]
然后我想获得具有此xpath +(/ h3)
的元素在HTML中他们是这样的:
<div class="results"><div class="text-center" id="result-error"><h3 class="one">
<div class="results"><div class="text-center" id="result-fail"><h3 class="one">
<div class="results"><div class="text-center" id="result-catchall"><h3 class="one">
<div class="results"><div class="text-center" id="result-success"><h3 class="one">
上面四个中的一个将是可见的。
修改 Python相当于你的代码:
wait = WebDriverWait(browser, 10)
rslts=wait.until(EC.presence_of_all_elements_located(By.CSS_SELECTOR, (".results h3 .one")))
然而我收到了这个错误:
rslts=wait.until(EC.presence_of_all_elements_located(By.CSS_SELECTOR, (".results h3 .one")))
TypeError: __init__() takes exactly 2 arguments (3 given)
我不确定你的css选择器和它们之间的空格。看起来presence_of_all_elements_located函数认为有3个参数而不是2.
答案 0 :(得分:0)
假设您使用的是Selenium Java绑定。
// A generic selector to grab a list of intended elements
//This should return all four div you have mentioned in the question
By byCss = By.cssSelector(".results h3 .one");
//Make sure all the elements loaded properly
List<WebElement> elements = (new WebDriverWait(driver,10)).until(ExpectedConditions.presenceOfAllElementsLocatedBy(byCss));
for (WebElement element: elements){
if (element.isDisplayed()){
//do whatever you want with the element
System.out.println(element.getText());
break;
}
}