How to locate the web element via sibling in python?

attachment

How to locate "check"in selenium, which corresponds to a button I need to click?

I can not locate it directly. I have to use id='animal'. There is no error if I try below, but there is no response.

driver.find_element_by_xpath('//div[@id="animal"]//following-sibling::div[contains(@style,"height:10px")]').find_element_by_xpath('//input[@value="check"]')

1 answer

  • answered 2018-01-14 08:54 Andersson

    You can use XPath

    //div[@id="animal"]/following-sibling::div/input[@value="check"]
    

    or modify your code as

    driver.find_element_by_xpath('//div[@id="animal"]/following-sibling::div[contains(@style,"height:10px")]').find_element_by_xpath('./input[@value="check"]')
    

    Note that I replaced //input which means input node somewhere on the page (returns the first found input in DOM) with ./input which means input node that is child of current node (returns the first found input in target div)