简体   繁体   中英

Trouble clicking a three way toggle switch using selenium webdriver with Python

I want to click a three way toggle using selenium with python.The html element Ids are dynamic. So I have tried with an XPath where class contains a specific text! But I seeing 'element not found'/'element not visible' whole day!

I've tried with below line of code but no help.

browser.find_element_by_xpath("//*[contains(@class,'switch switch-three toggle ios') and contains(text(),'Available')]").click()

Here is the HTML code of the page and I want to click on - 'Available'

<label class="switch switch-three toggle ios" id="radio8526" onclick="" style="float: left; width: 300px; height:15px; margin-right: 20px;margin-left: 20px;">          
    <input id="available8526" value="available" onclick="setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();;" name="onoffswitch8526" type="radio">                    
        <label for="available8526" onclick="">Available</label>
    <input id="unavailable8526" value="unavailable" onclick="setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();;" name="onoffswitch8526" type="radio">
        <label for="unavailable8526" onclick="">Unavailable</label>
    <input id="archived8526" value="archived" onclick="setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();;" name="onoffswitch8526" type="radio" checked="">
        <label for="archived8526" onclick="">Finalised</label>
<a class="slide-button"></a>
</label>

从w3c 文档您可以使用它来解决您的问题

browser.find_element_by_css_selector('input[id^="available"]').click()

You can just use the value attribute, eg

input[value='available']

You might need to add a wait for clickable to make sure the page has loaded. See this link for more info and some examples.

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