1

So im making a script that clicks some buttons for me in a web browser.

Importing the selenium driver:

from selenium import webdriver
browser = webdriver.Chrome(executable_path=r'C:/Python27/Scripts/chromedriver.exe')

In this example, When i want to click on the "leave hotel" button, i can just use the css_selector and it works fine.

<div id="canvas">  
<!--<a onclick="leave_hotel();" name="go_ut" onmousedown="signature.push([Math.floor(Date.now()), 'click']);" class="hotel-button-out">-->
<a onclick="leave_hotel();" name="go_ut" onmousedown="" class="hotel-button-out">Sjekk ut <span style="font-size:0.7em" id="hotell_tid">(23 t 55 m)</span></a>
        <input type="hidden" name="t" value="cb5a965fbb0a3578344aec059afa7246">
        <input type="hidden" name="xy" id="xy" value="">
        <input type="hidden" name="ltoken" value="ea782433046a2e254b10db5045b55d02">
        <input type="hidden" value="633eacc2bbe92004ba03c9dd57ff9457" name="ttoken">
        <input type="hidden" name="jtoken" id="jtoken" value=""> 
</div>

Something like this worked fine:

browser.find_element_by_css_selector('#canvas > a').click()

But when i try to click a button within a div on another page, it does not work. the html looks like this:

<div class="canvas hotel-out">
<input class=" btn btn-block" name="enkel_knapp" onmousedown="krim_enkel_bego(2);" type="button" id="submit" value="Begå kriminaliteten!">
<input type="hidden" name="ltoken" value="e386d141ff442655d48275e0269fe1fe">
<input type="hidden" value="2b61bc54fe84d18ae5ad797826152e32" name="ttoken">
<input type="hidden" name="jtoken" id="jtoken" value="">                        
<input type="hidden" id="xy" value="nytt system (ingen koordinater)" name="xy">
</div>

But the problem is that the buttons xpath is simliar to 3 other buttons. all three buttons got the xpath "//*[@id="submit"]". I guess there is some kind of conflict here?

When i try to do it by name like this i get no respose:

browser.find_element_by_name('enkel_knapp').click()

Nothing when i do it by ID either:

browser.find_element_by_id('submit').click()

Anyone got any tips to filter by div or something?

The error i get: Pastebin

22
  • can you try xpath //*[@id="submit"] in developer tool of browser to see this xpath can find only one or several elements? if find more than one elements try below code: browser.find_element_by_css_selector("div.canvas > input#submit") Commented Oct 10, 2017 at 23:03
  • Are you waiting for the element to load before trying to click it? stackoverflow.com/questions/30339643/… Commented Oct 10, 2017 at 23:06
  • Sorry guys, was still editing the post, can you just look over and see if anything changed? i will test what you guys just wrote asap. Commented Oct 10, 2017 at 23:07
  • @SlaterTyranus I manually type the command into the shell, long after the site has loaded and such, if that i what you meant? Im really not a python pro, neither a selenium pro, so sorry. Commented Oct 10, 2017 at 23:09
  • @yong When i type "xpath //*[@id="submit"]" in console i just get "Uncaught ReferenceError: xpath is not defined at <anonymous>:1:1" and "browser.find_element_by_css_selector("div.canvas > input#submit")" Gave me the same error as in pastebin. Commented Oct 10, 2017 at 23:13

1 Answer 1

1

As you mentioned in your Question that the xpath //*[@id="submit"] is not unique and similar to 3 other buttons hence we need to construct either a unique css_selector or a xpath for this button as follows:

  • css_selector

    browser.find_element_by_css_selector("div.canvas.hotel-out > input#submit").click()
    
  • xpath

    browser.find_element_by_xpath("//div[@class='canvas hotel-out']/input[@id='submit']").click()
    
Sign up to request clarification or add additional context in comments.

5 Comments

Still just get the same error as i do in the pastebin, or a very similar one. After a nights sleep i think that its important to mention that the site im trying to click buttons on is a php/js site, i never refresh the site or change the actuall url. I just change tabs within the same site, kindof.
hey @DebanjanB, i just found out that the button im trying to click is under some kind of "tab". When i right click the "tab" and open it as a new site, i get a more advanced url, and the whole website is just that tab. When i then try to .click() the button, it works just fine. How can i make the xpath go into that "url" and click the button without me having to have diffrent sites for diffrent "tabs"? Thanks alot for the help so far!
It just redirects me to a new .php site.
If we dont have browser object we can use webdriver object to access element by id.
@Maddy8381 Here the object referred as browser is not a browser type object, but actually the webdriver object. A lot of newbie users tends to name the webdriver object as browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.