简体   繁体   English

在Selenium Web驱动程序中使用Java脚本执行程序单击列表项

[英]click on list items using java script executor in selenium web driver

I have a requirement to collect multiple links and click on each link to extract some information from the website to the excel.So i have collected all links in one list and i am trying to click on each element depending on the index. 我需要收集多个链接,然后单击每个链接以从网站提取一些信息到excel.So,因此我已将所有链接收集在一个列表中,并且我试图根据索引单击每个元素。 I am not getting any exception and the click is also not performing on the element.I struck in clicking on link using javascript executor.Normal click and actions are not working here. 我没有任何异常,并且元素上的点击也没有执行。我使用javascript executor来点击链接。正常点击和操作在这里不起作用。

List<WebElement> titles=driver.findElements(By.xpath("//*[@class='product-name']"));
for(int i=0;i<titles.size();i++)
    {
    String title1=titles.get(i).getText();
    System.out.println(title1);
    Thread.sleep(5000);
        if(titles.get(i).isEnabled())
        {
        System.out.println("TAKE ACTION");
        js.executeScript("arguments[0].click();", titles.get(i));
        }
    }

According to your HTML you could collect all the href url instead of clicking in the header. 根据您的HTML,您可以收集所有href网址,而不用单击标题。

Sample code: 样例代码:

List<WebElement> elements = driver.findElements(By.xpath(".//h2[@class='product-name']//a"));
for (WebElement element : elements) {
    driver.navigate().to(element.getAttribute("href"));
}

暂无
暂无

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

相关问题 在 Selenium 中使用 Java Script Executor 时无法读取未定义的属性“click” - Cannot read property 'click' of undefined while using Java Script Executor in Selenium 如何使用selenium在Web中使用java脚本执行器在视口中垂直居中滚动元素 - How to scroll an element vertically center in view port using java script executor in web using selenium 在硒Web驱动程序中的节点js中单击列表的第二个元素 - Click on second element of the list in node js in selenium web driver 如何使用Selenium Web驱动程序单击导航栏 - How to click on navigation bar using selenium web driver 如何使用 JS Executor 单击 Web 元素并等待脚本执行? - How to click on a Web Element using JS Executor and wait until script executed? 通过Selenium Webdriver中的Java Script Executor传递复杂的xpath - Pass a complicated xpath through Java Script Executor in Selenium Webdriver 通过Selenium Webdriver中的Java脚本执行器传递变量 - Passing a variable through Java Script Executor in Selenium Webdriver WebDriverException:在硒Web驱动程序Java中未定义tinyMCE - WebDriverException: tinyMCE is not defined in selenium web driver java 如果在网页中进行Ajax请求或使用Selenium Web驱动程序拦截XMLHttpRequest,则使用Java Script进行跟踪 - Tracking with Java Script if Ajax request is going on in a webpage or Intercept XMLHttpRequest through Selenium Web driver 使用Selenium脚本的Javascript Executor在网页上水平滚动 - Scroll horizontally on a webpage using Javascript Executor for a Selenium script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM