用Python中的Selenium无法找到的按钮点击

2024-09-22 16:39:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个html元素:

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

因为某种原因我不能用我的代码点击它。。。 只是给了我一个关于不按类查找元素的错误。。。你知道吗

有没有一种方法可以发送“onclick”按钮的功能以及该按钮所包含的信息?i、 e发送此按钮的功能信息-“攻击(null,'6',2,0,')”

或者用这些信息找到按钮

我试过了,但好像找不到。。给我这个:

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

当我检查xpath时,每次打开页面时都会看到它的变化。。你知道吗

firstsecondthrid


Tags: 代码功能div信息元素html错误button
2条回答

我猜你有一个div元素,它的惟一id是'expedition\u info1'。您可以使用xpath Axes来单击与这个div元素相关的button。 尝试:

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

好的,我尽我所能地把这些放在一起,因为我不使用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

相关问题 更多 >