0

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>
Yeamin
  • 1
  • 2

2 Answers2

1

From w3c documentation You can use this to solve your problem

browser.find_element_by_css_selector('input[id^="available"]').click()
SushilG
  • 655
  • 1
  • 6
  • 19
  • Tried But confronted with below error again! selenium.common.exceptions.ElementNotVisibleException: Message: element not visible – Yeamin May 20 '19 at 07:09
  • You can try debugging to see if its finding the element without clicking it, there might be the chance that the element we are trying to click is the not visible and that's why its not clickable.There might be other elements like – SushilG May 20 '19 at 07:19
  • I think label is the element that is clickable , can you please try this and confirm browser.find_element_by_css_selector('label[for^="available"]').click() – SushilG May 20 '19 at 07:25
  • Tried but no help. – Yeamin May 20 '19 at 07:32
  • If nothing works then your last stop would be javascript executor . You can use the onclick values [onclick="setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();] and execute them with the help of javascript executor. – SushilG May 20 '19 at 08:30
  • The problem is - this Id here '8526', it will be dynamically changing. So I need a more generalized solution here.. – Yeamin May 20 '19 at 09:20
  • Then you can fetch that part using above code, above code is only throwing exception on clicking so instead of using .click just get the id="radio8526" attribute and then get id using substring. And then will be able to use js executor. – SushilG May 20 '19 at 09:28
  • Tried, this time no error has been thrown, rather the webpage is loaded and nothing happened. Tried with below code - browser.execute_script("setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();;") . Point to be noted here, same JS executer is defined for toggle state 'available/unavailable'. – Yeamin May 22 '19 at 10:34
  • Can you attach a screenshot of button and its html. Does html contains any custom-tags ? – SushilG May 23 '19 at 11:10
0

You can just use the value attribute, e.g.

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.

JeffC
  • 22,180
  • 5
  • 32
  • 55