我正在尝试使用以下html代码单击元素:
<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>
我需要单击值为“ Courier”的按钮
我正在尝试通过以下Python代码来实现:
elem = browser.find_element_by_css_selector("input[type='radio'][@value='Courier']")
elem.click()
(我也尝试通过xpath查找)
但是它说元素是不可见的。有什么方法可以点击此类元素?
答案 0 :(得分:0)
尝试使用此xPath:
//input[@type='radio' and @value='Courier']
还尝试使用WebDriverWait
:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='radio' and @value='Courier']"))).click()
注意: ,您必须添加一些导入:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
WebDriverWait
语句用于等待至少10秒钟,直到元素可单击,然后才单击它。
PS: 有关WebDriverWait
的更多信息,请访问here。
编辑: 尝试执行JavaScript:
radio_btn = driver.find_element_by_xpath("//input[@type='radio' and @value='Courier']")
driver.execute_script("arguments[0].click();", radio_btn)
答案 1 :(得分:0)
根据您共享的HTML <ul>
标记包含许多 JavaScript ,因此要对所需的元素调用click()
,必须诱导 WebDriverWait < / em>,使元素可点击,您可以使用以下任一解决方案:
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()