Issue
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[@class="css-e2pgpg"][@id='1h']')
actions.click(one_hour).perform()
Here's the html
#it is flexbox
<div class="css-e2pgpg">
<div id="Time" class="css-1pj8e72">Time</div>
<div id="15m" class="css-1pj8e72">15m</div>
<div id="1h" class="css-ktyfxp">1H</div> #i want to locate this element and perform click on it
<div id="4h" class="css-1pj8e72">4H</div>
<div id="1d" class="css-1pj8e72">1D</div>
<div id="1w" class="css-1pj8e72">1W</div>
</div>
Solution
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
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.