|
@@ -9,6 +9,8 @@ from selenium import webdriver
|
|
|
from selenium.common import exceptions
|
|
|
from selenium.webdriver.chrome.service import Service
|
|
|
from selenium.webdriver.common.by import By
|
|
|
+from selenium.webdriver.support import expected_conditions as EC
|
|
|
+from selenium.webdriver.support.wait import WebDriverWait
|
|
|
|
|
|
"""
|
|
|
根据table的id属性和table中的某一个元素定位其在table中的位置
|
|
@@ -63,6 +65,20 @@ def send_file(url, file_path):
|
|
|
response2 = requests.post(url, files=files)
|
|
|
return response2
|
|
|
|
|
|
+def get_element(my_driver, xpaths):
|
|
|
+ """
|
|
|
+ 判断是否存在元素并获取元素对象
|
|
|
+ :param my_driver:
|
|
|
+ :param xpaths: xpaths表达式
|
|
|
+ :return: 元素对象或为空
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ target = my_driver.find_element_by_xpath(xpaths)
|
|
|
+ except exceptions.NoSuchElementException:
|
|
|
+ return False
|
|
|
+ else:
|
|
|
+ return target
|
|
|
+
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
options = webdriver.ChromeOptions()
|
|
@@ -78,11 +94,16 @@ if __name__ == "__main__":
|
|
|
driver.get(
|
|
|
'https://www.cmegroup.com/markets/interest-rates/cme-fedwatch-tool.html?redirect=/trading/interest-rates/countdown-to-fomc.html')
|
|
|
sleep(5)
|
|
|
- try:
|
|
|
- driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/div/section/span').click()
|
|
|
- driver.find_element(By.XPATH, '//*[@id="onetrust-accept-btn-handler"]').click()
|
|
|
- except exceptions.NoSuchElementException:
|
|
|
- print("弹框不存在")
|
|
|
+ btn_pop = get_element(driver, '/html/body/div[4]/div[3]/div/section/span')
|
|
|
+ if btn_pop:
|
|
|
+ btn_pop.click()
|
|
|
+
|
|
|
+ accept_btn = get_element(driver, '//*[@id="onetrust-accept-btn-handler"]')
|
|
|
+ if accept_btn:
|
|
|
+ accept_btn.click()
|
|
|
+
|
|
|
+ WebDriverWait(driver, 10).until(
|
|
|
+ EC.visibility_of_element_located((By.XPATH, '//*[@id="cmeIframe-jtxelq2f"]')))
|
|
|
|
|
|
|
|
|
driver.execute_script("window.scrollBy(0,{})".format(600))
|