[英]Cannot find second element in Selenium even though it exists
I have a script which looks for a gym class name and then clicks on the appropriate button.我有一个脚本,用于查找健身房类名称,然后单击相应的按钮。 It works perfectly when there is one gym class of that name.当有一个同名的体育课时,它可以完美地工作。 But when there are two then I can not get it to choose the second.但是当有两个时,我无法选择第二个。
This is the script:这是脚本:
className = 'Zumba'
WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, "//p[contains(text(),'" + className + "')][2]/parent::div/parent::a[@onclick]")))
If I change the [2] to [1] then it works and selects the first element, but [2] does not select the second.如果我将 [2] 更改为 [1],那么它会起作用并选择第一个元素,但 [2] 不会选择第二个元素。
First element:第一个元素:
<div class="col-xs-12 col-sm-3 col-md-2 class column1" style="">
<a href="javascript:void(0)" style="text-decoration:none;" onclick="getClassDetails('265136', '614617')" class="studio"> <div>
<div class="triangle"></div>
<p style="padding:10px 25px 0 25px;font-size:13px">Zumba</p>
<p style="font-size:12px;text-align:left;float:left;padding:0;margin:0;">
350 cals
</p>
<p style="font-size:12px;text-align:right;float:right;padding:0;margin:0;">
45 mins
</p>
</div>
<div style="clear: both; font-size: 11px; text-align: center; padding: 0px; margin:-5px 5px 5px; background-color: #C52B2B; box-shadow: 0px 2px 2px -1px rgba(0, 0, 0, 0.55) inset;">Fully Booked</div>
</a>
</div>
Second element:第二个要素:
<div class="col-xs-12 col-sm-3 col-md-2 class column1" style="">
<a href="javascript:void(0)" style="text-decoration:none;" onclick="getClassDetails('265135', '614617')" class="studio"> <div>
<div class="triangle"></div>
<p style="padding:10px 25px 0 25px;font-size:13px">Zumba</p>
<p style="font-size:12px;text-align:left;float:left;padding:0;margin:0;">
350 cals
</p>
<p style="font-size:12px;text-align:right;float:right;padding:0;margin:0;">
45 mins
</p>
</div>
<div style="clear: both; font-size: 11px; text-align: center; padding: 0px; margin:-5px 5px 5px; background-color: #C52B2B; box-shadow: 0px 2px 2px -1px rgba(0, 0, 0, 0.55) inset;">Fully Booked</div>
</a>
<a href="javascript:void(0)" style="text-decoration:none;" onclick="getClassDetails('265185', '614617')" class="spinstudio"> <div>
<div class="triangle"></div>
<p style="padding:10px 25px 0 25px;font-size:13px">Indoor Cycling </p>
<p style="font-size:12px;text-align:left;float:left;padding:0;margin:0;">
400 cals
</p>
<p style="font-size:12px;text-align:right;float:right;padding:0;margin:0;">
45 mins
</p>
</div>
<div style="clear: both; font-size: 11px; text-align: center; padding: 0px; margin: -5px 5px 5px; background-color: #2d7698; box-shadow: 0px 2px 2px -1px rgba(0, 0, 0, 0.55) inset;">Join Waiting List</div>
</a>
</div>
Any help to troubleshoot would be welcomed.欢迎任何帮助排除故障。
Try尝试
WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, "(//p[contains(text(),'" + className + "')])[2]/parent::div/parent::a[@onclick]")))
The trick is to enclose (//p[contains(text(),'" + className + "')])
before selecting nth node.诀窍是在选择第 n 个节点之前将(//p[contains(text(),'" + className + "')])
括起来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.