在python中单击selenium无法找到的按钮

在python中单击selenium无法找到的按钮,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我有这个html元素: <button class="expedition_button awesome-button " onclick="attack(null, '6', 2, 0, '')"><div></div><div></div></button> 当我检查xpath时,我看到它在每次打开页面时都会发生变化 我猜您有一个具有唯一id“expedition\u info1”的div元素。您可以使用单击与此di

我有这个html元素:

<button class="expedition_button awesome-button " onclick="attack(null, '6', 2, 0, '')"><div></div><div></div></button>
当我检查xpath时,我看到它在每次打开页面时都会发生变化


我猜您有一个具有唯一id“expedition\u info1”的div元素。您可以使用单击与此
div
元素相关的
按钮。
尝试:


好的,我尽我所能把它们放在一起,因为我不是用Python编写代码的,所以我的语法在某些地方可能不正确,但我试着做对了,在这两种情况下,逻辑都在那里,注释应该解释我在做什么:

首先让我们获取容器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

请发布您用来尝试按类名浏览器查找元素的代码。按类名查找元素(“科考”按钮“很棒的按钮”),但这并不重要,因为有4个按钮具有相同的类名。。每一个都是div的子类,并且具有相同的类名<代码>按类名称查找元素
函数只允许一个类参数。根本没有帮助…显示其他按钮元素的完整html或html代码。我的错,它应该在同级之后,现在再试一次。@omerb这会检查按钮的文本,不是div。我认为每个按钮的按钮文本是不同的,否则有4个按钮有什么意义?每个按钮用于攻击游戏中的npc,每个容器都有自己的npc,下面有一个要攻击的按钮。所有按钮的文本都是相同的。@omerb然后检查容器中npc的名称,如果满足该条件,深入到按钮,您可以修改我给您的内容并实现您的目标
driver.find_element_by_xpath("//div[@id='expedition_info1']/following-sibling::*[1]/button")// to click on 1st button element.
//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