Artikel ini memperkenalkan bagaimana kami menggunakan selenium untuk memanipulasi berbagai elemen halaman
Konten membaca
Tautan (tautan)
<div> <p> Tautan tautan </p> <a href = "www.cnblogs.com/tankxiao"> Tank kecil </a> </div>
Operasi tautan
// Temukan tautan elemen webElement link1 = driver.findElement (by.linkText ("Tank Kecil")); WebElement link11 = driver.findElement (by.partiAllinkText ("Tank")); // klik tautan link1.click ();Input Box TextBox
<dv> <p> kotak input testbox </p> <input type = "text" id = "usernameid" value = "username"/> </div>
Pengoperasian kotak input
// Temukan elemen elemen WebElement = driver.findElement (by.id ("UserNameId")); // masukkan elemen konten.sendkeys ("test111111"); // Hapus input box element.clear (); // Dapatkan konten dari kotak input elemen.getAttribute ("value");Tombol
<pv> <p> Tombol tombol </p> <input type = "Tombol" value = "add" id = "proAditem_0"/> </div>
Temukan Elemen Tombol
// Temukan string elemen tombol xpath = "// input [@value = 'add']"; WebElement addButton = driver.findElement (by.xpath (xpath)); // Klik tombol AddButton.Click (); // Tentukan apakah tombol memungkinkan addbutton.isenabled ();
Tarik Kotak Pilihan (Pilih)
<div> <p>Drop-down selection box Select</p> <select id="proAddItem_kind" name="kind"> <option value="1">Computer hardware</option> <option value="2">Real Estate</option> <option value="18">Category AA</option> <option value="19">Category BB</option> <option value="20">Category BB</option> <option value = "21"> Kategori CC </tiption> </tect> </div>
Operasi kotak pemilihan drop-down
// Temukan elemen pilih pilih = baru pilih (driver.findeLement (by.id ("proadditem_kind"))); // Pilih pilihan yang sesuai, indeks mulai dari 0 select.selectbyIndex (2); pilih. SelectByValue ("18"); pilih. // Dapatkan semua Daftar Opsi <Sebelement> opsi = select.getOptions (); for (WebElement WebElement: Options) {System.out.println (WebElement.getText ()); }Tombol radio
<div> <p>Single-option Radio Button</p> <input type="radio" value="Apple" name="fruit>" />Apple <input type="radio" value="Pear" name="fruit>" />Pear <input type="radio" value="Banana" name="fruit>" />Banana <input type="radio" value="Orange" name="fruit>" />Orange </div>
Pengoperasian elemen opsi tunggal
// Temukan elemen kotak radio string xpath = "// input [@type = 'Radio'] [@value = 'apple']"; WebElement apple = driver.findElement (by.xpath (xpath)); // pilih kotak radio apple.click (); // menilai apakah kotak radio telah dipilih boolean isappleselect = apple.isselected (); // Dapatkan atribut elemen apple.getAttribute ("value");Beberapa kotak centang
<div> <p>Multi-option checkbox</p> <input type="checkbox" value="Apple" name="fruit>" />Apple <input type="checkbox" value="Pear" name="fruit>" />Pear <input type="checkbox" value="Banana" name="fruit>" />Banana <input type="checkbox" value="Orange" name="fruit>" />Orange </div>
Pengoperasian kotak multi-check persis sama dengan kotak satu-cek, jadi saya tidak akan membicarakannya di sini.
Di atas adalah informasi tentang operasi elemen Web UI umum Java Selenium. Kami akan terus menambahkannya nanti. Terima kasih atas dukungan Anda untuk situs ini!