【问题标题】:How do I move hover to the following element and then click on the visible button?如何将悬停移动到以下元素,然后单击可见按钮?
【发布时间】:2018-10-12 05:12:20
【问题描述】:

这是我希望鼠标移动的元素。 <div id="div282" class="divAsset">

鼠标悬停时,一些元素可见,我想点击

<a class="tileDownloadButton button" title="Download" target="_blank" href="xxxxx"> Download </a>

如何将鼠标悬停在以下元素上,然后单击可见按钮?

【问题讨论】:

  • 可以使用Action类来执行

标签: selenium selenium-webdriver


【解决方案1】:

Python

asset = driver.find_element_by_id("div282")
action.move_to_element(asset).perform();
button = driver.find_element_by_xpath("//a[contains(text(),'Download')]");
button.click()

Java

Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.id("div282"))).perform();

driver.findElement(By.xpath("//a[contains(text(),'Download')]")).click();

【讨论】:

    【解决方案2】:

    您可以执行 jquery 将鼠标悬停在元素上。

    python 中的示例。

    driver.execute_script("$('.classname').trigger('mouseover')")  #selecting element based on classname.
    

    Java 中的示例:

    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("$('.classname').trigger('mouseover')");
    

    这将使元素可见,然后您可以轻松单击元素。

    查看其他 jquery 选择器here

    【讨论】:

    • 先生,您能解释一下Java中的“.classname”示例吗?
    • ".classname" 是您的 html 中的类名,例如:class="tileDownloadButton button" 您需要将“.classname”替换为“.tileDownloadButton button”
    • 谢谢先生,感谢您的回答!
    猜你喜欢
    • 2013-05-25
    • 2020-12-16
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 2021-11-10
    • 2020-10-03
    • 1970-01-01
    相关资源
    最近更新 更多