Home > Software engineering >  How to perform click on element that inside flexbox
How to perform click on element that inside flexbox

Time:01-27

I'm working on a web-scraping project , I encounter a problem that I couldn't locate the element(1H) by using find_element_by_xpath/id/css-selector/class_name and perform click()on it. Does anyone have any ideas how to make it work ? Thanks in advance!

Here's the part of my code

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from datetime import timedelta, date
import time
import datetime
import re
import mouse

def scrape(): 
    website = 'https://www.binance.com/en/trade-margin/BTC_USDT'
    path = '/Users/admin/Downloads/chromedriver'
    driver = webdriver.Chrome(path)
    driver.get(website) 
    driver.maximize_window()
    
    #click on 1H
    actions = ActionChains(driver)
    one_hour = driver.find_element_by_xpath('//div[@][@id='1h']')
    actions.click(one_hour).perform() 

Here's the html

#it is flexbox
<div >
<div id="Time" >Time</div>
<div id="15m" >15m</div>
<div id="1h" >1H</div> #i want to locate this element and perform click on it 
<div id="4h" >4H</div>
<div id="1d" >1D</div>
<div id="1w" >1W</div>
</div>

CodePudding user response:

If you are just looking to click on 1H web element, you can do it by using the below code. We have to induce explicit wait to get the job done.

driver.maximize_window()
wait = WebDriverWait(driver, 30)

driver.get("https://www.binance.com/en/trade-margin/BTC_USDT")

try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='1H']"))).click()
    print("Clicked")
except:
    pass

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

CodePudding user response:

I went to see the webpage myself. In my opinion, you don't have to locate the flexbox itself.

Just try with:

driver.find_element_by_id('1h').click();

Also, I would add some kind of wait until the page loads fully. You can use Thread.sleep, implicit waits or waiting for an element.

  •  Tags:  
  • Related