简体   繁体   中英

How to click on a Radio Button as per the HTML using Selenium and Python

I am trying to click on element with following html code:

<div class="delivery-pseudo-table">
<div class=" i-popup-lk user-address" id="userAddress">
<div class="address-container">
<div class="titlebar">
<h1 class="title"></h1>
</div>
<div id="userAddressContent"></div>
</div>
<div class="hide j-map map" id="addressMap"></div>
<div class="hide j-map-pickpoint postamat-map" id="pickpointMap"></div>
<div class="added-addres-loader"></div>
</div>
<ul class="delivery-method" data-jsv-df=""><li class="selfDelivery selectDeliveryWay active" data-jsv="#34_#35_" title="">
<script type="jsv#37_"></script>
<label><input autocomplete="off" checked="checked" data-id="2" name="orderDetails.DeliveryWay_q" type="radio" value="Self"/>Самовывоз</label>
<script type="jsv/37_"></script>
</li><li class="courierDelivery selectDeliveryWay" data-jsv="/35_#38_" title="">
<script type="jsv#39_"></script>
<label><input autocomplete="off" data-id="1" name="orderDetails.DeliveryWay_q" type="radio" value="Courier"/>Доставка курьером</label>
<script type="jsv/39_"></script>
</li><li class="wbpostamatDelivery selectDeliveryWay" data-jsv="/38_#40_" title="">
<script type="jsv#41_"></script>
<label><input autocomplete="off" data-id="16" name="orderDetails.DeliveryWay_q" type="radio" value="WbPostamat"/>Постамат</label>
<script type="jsv/41_"></script>
</li></ul>

I need to click on button with value "Courier"

And I am trying to do it with following Python code:

elem = browser.find_element_by_css_selector("input[type='radio'][@value='Courier']")
elem.click()

(I've also tried to find by xpath)

But it says that the element is invisible. Are the any ways to click on such element?

Try to use this xPath:

//input[@type='radio' and @value='Courier']

also try to use WebDriverWait :

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='radio' and @value='Courier']"))).click()

Note: you have to add some imports:

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

WebDriverWait statement is for waiting at least 10 seconds until element will be clickable and only then clicks on it.

PS: more information about WebDriverWait can be found here .

EDIT: try to execute JavaScript:

radio_btn = driver.find_element_by_xpath("//input[@type='radio' and @value='Courier']")
driver.execute_script("arguments[0].click();", radio_btn)

As per the HTML you have shared the <ul> tag contains a lot of JavaScripts so to invoke click() on the desired element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.delivery-method li.courierDelivery.selectDeliveryWay input[value='Courier']"))).click() 
  • XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='delivery-method']//li[@class='courierDelivery selectDeliveryWay']//input[@value='Courier']"))).click() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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