Selenium webdriver click vs action.click whats the difference?

前端 未结 2 1894
攒了一身酷
攒了一身酷 2021-01-05 01:17

I was struggling with a popup dialog in a website I\'m automating, for some reason it wouldn\'t click a button in the dialog but interacted fine with everything else fine.

相关标签:
2条回答
  • 2021-01-05 02:10

    Just testing similar scenario.

    First with Actions click:

    actions.moveToElement(driver.findElement(By.xpath("//*[@id='relevantJobsAndCareerUpdates_1']"))).click().perform()
    

    And second one with WebElement click:

    driver.findElement(By.xpath("//*[@id='relevantJobsAndCareerUpdates_1']"))).click()
    

    The second one does not work. It gives the error message:

    Root cause: org.openqa.selenium.WebDriverException: unknown error: Element <input data-val-required="Information required.
    " id="relevantJobsAndCareerUpdates_1" name="OptiInEmailJobsAndCarrerRelated" type="radio" value="1"> is not clickable at point (307, 24). Other element would receive the click: <label for="relevantJobsAndCareerUpdates_1" class="control-label">...</label>
    

    This is the similar problem as previous. Using Actions solve this problem.

    0 讨论(0)
  • 2021-01-05 02:14

    I am not sure about your specific case, but there are several differences between WebElement click method and Actions click method. Actions' click is a lot dumber, it pretty much just sends the click event to the element (location) that you pass in. It does not care about the element, it just does the click and moves forward whereas webelement click is a blocking call (not always, check the references) and it also has preconditions like the WebElement to be clicked must be visible. Also, webElements' click is a void method, actions click returns reference to the Actions you are using. For more information check here and here.


    edit. Looking at the markup you posted, and it can be totally wrong as I am not a boss on bootstrap CSS, the modal hide fade in and especially the fade in part there looks suspicious. Are you sure that when you send the webelement.click(), your element is in clickable state? What happens? Nothing? Then again, if the actions click is reliably working, why not just go with it, I mean, if something works, why fix it?

    0 讨论(0)
提交回复
热议问题