Home > OS >  Using python and selenium to click a button on a web page
Using python and selenium to click a button on a web page

Time:01-04

enter image description here

So I'm trying to connect to https://coinmarketcap.com/currencies/bitcoin/ and press the good button which is 3/4;s of the way down.

Using Selenium I've tried to use the get_element(By.xpath) and other attributes, when looking at inspect I dont see what I can use as the bottons look the exact same in config.

should I be using JavaScript to execute the click? how would you go about selecting them with them seeming to share the same attributes. what would be the code to click good and what would it be to click bad, I assume its [0] or [1] is some form of Js?

used selenium attributes, I used By.XPATH, '//*[@id="__next"]/div/div[1]/div[2]/div/div[3]/div/div[1]/div[2]/div[6]/div/div[2]/div/button[1]'

as that seemed as it should work, but I receive. return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]

Thanks for any advice in advance.

CodePudding user response:

As you have the element ID, it is best to search the element by ID:

goodButton = browser.find_element(By.ID,"__next")

This will make sure you always select this element even if it gets displaced from existing location. For example, an advertisement banner is inserted above it.

As a rule of thumb remember that a webpage is expected to have unique ID for any element.

CodePudding user response:

Try this:

button_class = BeautifulSoup(driver.page_source, 'html.parser').select('div', class_='button-group')[0].button['class']
button_class = ' '.join(button_class)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, f"//button[@class='{button_class}']"))).click()
  • Related