简体   繁体   English

如何使用 python 单击位于 selenium 下的无线电输入角色

[英]How to click an input role as radio located under in selenium using python

I am doing some automation script for the website https://silkdb.bioinfotoolkits.net in selenium using python language我正在使用 python 语言在 selenium 中为网站https://silkdb.bioinfotoolkits.net做一些自动化脚本在此处输入图像描述 where I have you can see a button named "Blast" that pops up a window with some default values on click.我在哪里你可以看到一个名为“Blast”的按钮,它会弹出一个 window,点击时有一些默认值。

<div data-meta="Field" id="program" role="radiogroup" class="next-radio-group next-radio-button next-radio-button-medium">
<label dir="ltr" aria-checked="true" class="next-radio-wrapper checked "><span class="next-radio-single-input">
   <input role="radio" tabindex="0" type="radio" aria-checked="true" class="next-radio-input"></span><span class="next-radio-label">blastn</span>
</label>

<label dir="ltr" aria-checked="false" class="next-radio-wrapper "><span class="next-radio-single-input">
<input role="radio" tabindex="-1" type="radio" aria-checked="false" class="next-radio-input"></span><span class="next-radio-label">blastx</span></label>

<label dir="ltr" aria-checked="false" class="next-radio-wrapper "><span class="next-radio-single-input"><input role="radio" tabindex="-1" type="radio" aria-checked="false" class="next-radio-input"></span><span class="next-radio-label">tblastn</span></label>

<label dir="ltr" aria-checked="false" class="next-radio-wrapper "><span class="next-radio-single-input"><input role="radio" tabindex="-1" type="radio" aria-checked="false" class="next-radio-input"></span><span class="next-radio-label">blastp</span></label></div>

The default label named "blastn" is selected on defualt.默认选择名为“blastn”的默认 label。 But I want to select lable named "blastp".但我想将 select 标签命名为“blastp”。

I have tried many things using xpath but I am unable to get it selected.我使用 xpath 尝试了很多事情,但我无法选择它。

Please help me in this.请帮助我。 在此处输入图像描述

To click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :要单击元素,您必须为element_to_be_clickable()诱导WebDriverWait ,并且您可以使用以下任一Locator Strategies

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     driver.get("https://silkdb.bioinfotoolkits.net/") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#btn-blast"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#program label:last-child span:nth-child(2)"))).click()
  • Using XPATH :使用XPATH

     driver.get("https://silkdb.bioinfotoolkits.net/") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='btn-blast']"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='next-radio-label' and text()='blastp']"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

Browser Snapshot:浏览器快照:

爆炸

It is important to send the click events to the appropriate receiving elements which can actually react to the click event.将点击事件发送到可以实际响应点击事件的适当接收元素是很重要的。 In this case, i think the click must be send to the input element, not the span.在这种情况下,我认为点击必须发送到输入元素,而不是跨度。 Please try with the following xpath.请尝试使用以下 xpath。

//span[@class="next-radio-label" and contains(text(),'blastp')]/preceding-sibling::span/input

Please use following xpath to click your option //span[contains(text(),'blastp')]请使用以下 xpath 单击您的选项//span[contains(text(),'blastp')]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM