简体   繁体   English

即使存在,也无法在 Selenium 中找到第二个元素

[英]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.

相关问题 即使元素存在,Selenium find_element 也会抛出异常 - Selenium find_element throwing exception even though the element exists 无法在 selenium chromedriver 中找到元素,即使它存在 - Unable to locate element in selenium chromedriver even though it exists Python:即使存在列表元素也无法访问 - Python : Cannot access list element even though it exists Python selenium 即使等待也找不到元素 - Python selenium cannot find element even with wait Selenium WebDriver即使看起来似乎也无法单击元素 - Selenium WebDriver cannot click element even though it seems to be visible Python Selenium,即使存在,也无法按 id 查找和单击元素 - Python Selenium, cant find and click element by id even if it exists 使用Selenium和python,即使元素存在但实际上不可见,如何检查 - Using selenium and python, how to check that even though an element exists but it is not actually visible Beautiful Soup find() function 即使元素存在并且 find() 适用于页面上的其他元素,也不返回任何内容? - Beautiful Soup find() function returning none even though element exists and find() works for other elements on the page? Selenium ChromeDriver:找不到我知道存在的 web 元素 - Selenium ChromeDriver: Cannot find web element I know exists 即使元素存在,BeautifulSoup也返回None - BeautifulSoup returns None even though the element exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM