Python- Selenium : How to click on the element with text as "Year" within the webpage

标签 python selenium selenium-webdriver xpath css-selectors

from bs4 import BeautifulSoup as soup
from openpyxl import load_workbook
from openpyxl.styles import PatternFill, Font
from selenium import webdriver
from selenium.webdriver.common.by import By
import datetime
import os
import time
import re

browser = webdriver.Chrome(executable_path=r"C:\Program Files(x86)\Google\Chrome\Application\chromedriver.exe")
my_url = 'https://www.eex.com/en/market-data/power/futures/phelix-at-futures#!/2017/07/24'
browser.get(my_url)
button = browser.find_elements_by_class_name('ng-scope')[-1]
browser.execute_script("arguments[0].click();", button)

它没有给我任何错误,但实际上并没有点击。它应该逐月变化,因为它是最后一个元素,但它保留在那里。

最佳答案

根据您提供的url,单击文本为年份的元素,您必须为引发WebDriverWait可点击的元素如下:

  • CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.tabs.filter_wrap.clearfix li.ng-scope:nth-child(3)>a"))).click()
    
  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='tabs filter_wrap clearfix']//li[@class='ng-scope']/a[contains(.,'Year')]"))).click()
    

关于Python- Selenium : How to click on the element with text as "Year" within the webpage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51149507/

相关文章:

python - 如何在不混合相应条目的情况下将 2D 矩阵转换为 3D 张量?

javascript - Protractor 本地和全局安装的行为不同

selenium - 如何使用Selenium Webdriver中的xpath查找包含的文本的确切值?

python - Selenium 无法访问Youtube搜索栏?

java - Selenium WebDriver 在选择 xpath 时抛出错误

python - 从 .py 文件中获取包根和完整模块名称以进行导入

python - Pandas 与不同频率的数据帧合并(每小时和每天)

Python读取带有换行符和段落分隔元素的文本文件

python - 使用 Python 和 Selenium 进行抓取 - 如果元素不存在,我应该如何返回 'null'

html - 无法定位带有关键字结尾的输入框 - Xpath