简体   繁体   English

Java Selenium Webdriver Chrome无法找到元素

[英]Java Selenium Webdriver chrome can't find element

I am trying to click on a on card inside a container. 我试图单击容器内的卡片。 I have tried id, class,xpath but it doesn't find it. 我尝试了id,class,xpath,但找不到。 I unsure on what else to try to be able to get the element. 我不确定还有什么其他方法可以获取该元素。

here is the code of the element I want to click on: 这是我要单击的元素的代码:

<div class="mtable" id="w-card" status="83" entertainer="1799">
    <div class="mtable-cell wc-gray-dark" id="widget-icon" style="border-color: ">
        <div class="caption"><img src="/images/default_entertainer_image.jpg" class="caption-img"></div>
    </div>
    <div class="mtable-cell wc-white" id="widget-text">
        <div>
            <h6>
                <span id="status-83" class="label label-default ">
                    <span class="entertainer-name">Testing</span>
                    <br>Testing100
                    <br>Last Seen: never
                </span>
            </h6>
        </div>
        <div id="timers">
            <div id="lastChange" class="pull-left"></div>
            <div id="circle" class="pull-right"></div>
        </div>
    </div>
</div>

selenium code: 硒代码:

driver.findElement(By.xpath("//*[@id=\"w-card\"]")).click();

I have run display tests on all of the child elements of the div as well webdriver is finding it but is saying it's not displayed? 我已经在div的所有子元素上运行了显示测试,以及webdriver正在找到它,但是说它没有显示?

Use Explicit Wait to wait for element to be clickable. 使用“ 显式等待”以等待元素可单击。

WebDriverWait driverWait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.elementToBeClickable(By.id("w-card"))).click();

Try to use explicit wait 尝试使用显式等待

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("w-card"))).click();

This will wait up to 20 seconds for the element to be visible before clicking on it. 单击该元素之前,最多需要等待20秒才能看到该元素。

I think your Div is not visible. 我认为您的Div不可见。 So do few things run a for loop with 3 sec interval for like 10 iterations and inside that check for isDisplayed() condition, does it ever gets displayed. 因此,很少有事情以3秒钟的间隔运行一次for循环,进行10次迭代,并在其中检查isDisplayed()条件是否得到显示。 Also if everything fails then try javascript click..thats never recommended but still saves us 另外,如果一切都失败了,请尝试javascript click ..

也许有一个iframe ,所以切换到该框架。

        `driver.switchTo().frame(driver.findElement(by));`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM