简体   繁体   中英

How do I select an expandable item in selenium

I am trying to select an element on a webpage with selenium and am having no success. It is an expandable item that makes other items/fields visible when clicked.

Code for element:

<DIV class="section">
    <H1 class="sectionHeader collapsed" onclick="toggleSection('ResolutionSection',this,'YES')">Resolution Section</H1>
        <span style="width:100%;text-align: right;">
            <A HREF="#top" class="toplink">
                Go to top
            </A>

Selenium Code I have tried:

driver.findElement(By.xpath("/html/body/form/div[2]/div[5]/div/table/tbody/tr/td/div/div[1]/div[4]/h1")).click();
driver.findElement(By.xpath("//div[@id='documentLayoutSection']/div[4]/h1")).click()

Errors I have gotten:

Jun 15, 2014 7:45:18 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: I/O exception (java.net.SocketException) caught when processing request: Software caused connection abort: recv failed

OR cannot find element.

Does anyone have any suggestions?

Thanks

You can try to use an Actions chain:

(new Actions(driver))
    .moveToElement(driver.findElement(By.cssSelector("h1.sectionHeader.collapsed")))
    .click()
    .build()
    .perform();

The onclick javascript event may be changing the page in a way that makes the page difficult for Selenium to interact with. After a click , Selenium expects to be able to interact with the element again. If the element changes during the onclick javascript event, Selenium is not able to get a response back from the element after the click .

This makes Selenium very sad.

Since this Actions chain is built to click where the mouse currently is, it may work.

Failing that, I think we're going to need to see more HTML, or better yet, a link to the page if possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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