0

I have this html element:

<button class="expedition_button awesome-button " onclick="attack(null, '6', 2, 0, '')"><div></div><div></div></button>

and I cant click on it for some reason with my code... just gives me an error about not finding the element by class...

Is there a way sending the function of the button "onclick" the information the button hold? i.e sending the function of this button the information - "attack(null, '6', 2, 0, '')"

or finding the button using this information

Tried xpath but I cant seem to find it.. just gives me this:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div[2]/div/div[3]/div/div[2]/div[2]/div[2]/div[2]/div[2]/button

When I check the xpath I see it changes every time the page opens up..

first second thrid

Seanny123
  • 8,776
  • 13
  • 68
  • 124
omerb
  • 39
  • 1
  • 8
  • Please post what code you were using to try and find the element by className – Ryan Wilson Aug 30 '18 at 16:14
  • browser.find_element_by_class_name('expedition_button awesome-button') but it really doesnt matter since there is 4 buttons with the same class name.. each one is a child of div with the same class names as well – omerb Aug 30 '18 at 16:18
  • Checkout https://stackoverflow.com/questions/37771604/selenium-compound-class-names-not-permitted ... `find_element_by_class_name` function only allows for one class parameter. – Gary Thomas Aug 30 '18 at 16:21
  • didnt help at all.... – omerb Aug 30 '18 at 16:22
  • Is there anything in the html which indicates the button you want to click on such as a label or text? – Ryan Wilson Aug 30 '18 at 16:22
  • the thing is like this, there are 4 buttons exactly the same just under different container's that each one sends differenet information to a onclick function... – omerb Aug 30 '18 at 16:24
  • 1
    Show us full html or your html code for other button elements. – theGuy Aug 30 '18 at 16:24
  • @omerb I understand the structure, but certainly there is some kind of "label or text" which indicates what clicking one of the 4 buttons will do. As theGuy said, post the relevant Html. – Ryan Wilson Aug 30 '18 at 16:26
  • https://ibb.co/e71v59 https://ibb.co/jkZtsp https://ibb.co/cqfBdU – omerb Aug 30 '18 at 16:29
  • as you can see there is a div id of expedition_info1,2,3,4 but the child of it is not found... they are the same the only thing changing is the expedition_info1,2,3,4 and the information the button sends to the function onclick – omerb Aug 30 '18 at 16:30
  • @omerb People are not going to want to click on some outside resource link that your provided, post the HTML in your question. – Ryan Wilson Aug 30 '18 at 16:33
  • its an uploading site to an image... the html code is too big to post here – omerb Aug 30 '18 at 16:35
  • added the image via stackoverflow imgur for your request.. – omerb Aug 30 '18 at 16:48

2 Answers2

0

I guess you have a div element with unique id 'expedition_info1'. You can use xpath Axes to click on button in relation to this div element. Try:

driver.find_element_by_xpath("//div[@id='expedition_info1']/following-sibling::*[1]/button")// to click on 1st button element.
theGuy
  • 683
  • 5
  • 15
0

Ok, so I put this together as best as I can, as I don't code in Python, so my syntax may be incorrect in certain places, but I tried to do it right, in either case the logic is there and the comments should explain what I am doing:

First lets get the container div:

//Get the container which holds the 4 divs and their buttons
containerDiv = browser.find_element_by_id('expedition_list')

//Get the divs which contain the buttons
containerDivs = containerDiv.find_elements_by_class_name('expedition_box')

//Iterate the containers
for val in containerDivs:
    //Get the div elements of the container
    childDivs = val.find_elements_by_tag_name('div')
    //iterate these divs, looking for the one which contains a button
    for val1 in childDivs
       buttonDiv = val1.find_elements_by_tag_name('button')
       //Check to see if we found a div with a button
       if len(buttonDiv) > 0
          for button in buttonDiv
          //If the button text is what we are looking for, click it
          if button.Text = 'whatever your button's text is'
             button.Click
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
  • @omerb This checks the text of the button, not the div. I would think that the button text is different for each button or else what would be the point in having 4 buttons? – Ryan Wilson Aug 30 '18 at 18:05
  • each button is used to attack an npc in a game, each container has its own npc with a button to attack under it. the text is the same for all the button's... – omerb Aug 30 '18 at 20:00
  • @omerb Then check for the name of the npc in the container and if that condition is met, drill down to the button, you could modify what I gave you and accomplish your goal – Ryan Wilson Aug 30 '18 at 20:03