如何在每次运行中使用Selenium单击其他项目?

时间:2014-06-07 00:01:00

标签: java list selenium selenium-webdriver html-lists

我有一个列表,并且每次运行测试时都想点击其他项目。 有什么更好的方法呢?

<ul class="vitrineProdutos">
<li id="ctl00_Conteudo_PaginaSistemaArea1_celular-desbloqueado-nokia-lumia-520-preto-com-windows-phone-8-tela-de-4-processador-dual-core-camera-5mp-3g-wi-fi-bluetooth-e-a-gps-tim-1970880" class="celulares---telefones first">
<li id="ctl00_Conteudo_PaginaSistemaArea1_aparelho-de-pressao-de-pulso-premium-automatico-bplp200-1641073" class="saude---beleza">
<li id="ctl00_Conteudo_PaginaSistemaArea1_smart-tv-led-42-full-hd-lg-42lb5800-com-funcao-torcida-conversor-digital-wi-fi-entradas-usb-e-hdmi-3073030" class="eletronicos last">
<li id="ctl00_Conteudo_PaginaSistemaArea1_lavadora-de-roupas-brastemp-11-kg-ative-bwg11ab-com-sistema-fast-1736552" class="eletrodomesticos first">
<li id="ctl00_Conteudo_PaginaSistemaArea1_figurinhas-da-copa-de-2014-total-72-figurinhas-sortidas-3053702" class="livros">
<li id="ctl00_Conteudo_PaginaSistemaArea1_cozinha-compacta-itatiaia-anita-smart-com-3-pecas-1599236" class="moveis last">
<li id="ctl00_Conteudo_PaginaSistemaArea1_travesseiro-fibrasca-visco-nasa-bege-1691344" class="cama-mesa-e-banho first">
<li id="ctl00_Conteudo_PaginaSistemaArea1_conjunto-de-panelas-panex-day-by-day-7-pecas-425715" class="utilidades-domesticas">
<li id="ctl00_Conteudo_PaginaSistemaArea1_mini-cama-elastica-sport-camping-board-1064-cinza-garrafa-squeeze-sport-camping-500-ml-174640" class="esporte---lazer last">
<li id="ctl00_Conteudo_PaginaSistemaArea1_tablet-samsung-galaxy-tab-3-com-tela-7-sm-t2100-com-8gb-processador-dual-core-de-1-2ghz-camera-3mp-wi-fi-gps-e-android-4-1-–-branco-2233513" class="tablets first">
<li id="ctl00_Conteudo_PaginaSistemaArea1_mixer-philips-walita-viva-collection-com-3-acessorios-400w-852331" class="portateis">
<li id="ctl00_Conteudo_PaginaSistemaArea1_camera-digital-nikon-coolpix-l820-preta-16mp-lcd-3-0-zoom-otico-30x-foto-panoramica-e-3d-video-full-hd-cartao-de-4gb-2325643" class="cameras---filmadoras last">
<li id="ctl00_Conteudo_PaginaSistemaArea1_xbox-live-gold-3-meses-x360-263710" class="games first">
<li id="ctl00_Conteudo_PaginaSistemaArea1_conjunto-de-malas-fixxar-com-3-pecas-p-m-g-preto-1678029" class="malas">
<li id="ctl00_Conteudo_PaginaSistemaArea1_furadeira-parafusadeira-sem-fio-goodyear-gy-dc-17000-9-6v-2072634" class="ferramentas last">
<li id="ctl00_Conteudo_PaginaSistemaArea1_freezer-horizontal-electrolux-h400-385-l-6829" class="linha-industrial first">
<li id="ctl00_Conteudo_PaginaSistemaArea1_calculadora-financeira-hp-12c-gold-7132" class="papelaria">
<li id="ctl00_Conteudo_PaginaSistemaArea1_pen-drive-sandisk-cruzer-blade-8gb-175743" class="informatica last">
<li id="ctl00_Conteudo_PaginaSistemaArea1_pelucia-musical-multibrink-galinha-pintadinha-7520-851108" class="brinquedos first">
<li id="ctl00_Conteudo_PaginaSistemaArea1_cd-player-pioneer-deh-x1680ub-com-radio-am-fm-tecnologia-mixtrax-entradas-usb-auxiliar-e-rca-e-controle-remoto-2441089" class="automotivo">
<li id="ctl00_Conteudo_PaginaSistemaArea1_climatizador-de-ar-electrolux-cl07f-frio-com-controle-remoto-247865" class="ar-e-ventilacao last">
</ul>

2 个答案:

答案 0 :(得分:0)

// get all links by xpath
List<WebElement> links = driver.findElements(By.xpath("//li"));

// select a random one
Random gen = new Random();
WebElement link = links.get(gen.nextInt(list.size()));

link.click();

答案 1 :(得分:0)

尝试生成随机数并将该随机数传递给selenium代码以单击/选择

代码如下所示:

List<WebElement> allLinks = driver.findElements(By.xpath("//ul[@class='vitrineProdutos']/li"));

Random randomNumber = new Random();
randomNumber.nextInt(allLinks.size()-1)

driver.findElement(By.xpath("//ul[@class='vitrineProdutos']/li[randomNumber]")).click();

这里我们采用随机数的上限(“总列表项目” - 1)。 这是为了避免在随机执行时出现“Index out of Bound错误”异常。