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)
它没有给我任何错误,但实际上没有单击。由于它是最后一个元素,因此应该逐年更改,但它会保留在那里。
答案 0 :(得分:3)
根据您提供的 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()
答案 1 :(得分:1)
尝试一下:
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)
# wait until button will be present
WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.XPATH, "//*[@id='content']/div/div/ul/li[3]/a")))
# find the button and click on it
button = driver.find_element_by_xpath("//*[@id='content']/div/div/ul/li[3]/a")
button.click()
答案 2 :(得分:1)
您可以用不同的方式去皮。但是,在这种情况下,更好的方法是使用不太可能损坏的.find_element_by_link_text()
。试一试:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://www.eex.com/en/market-data/power/futures/phelix-at-futures#!/2017/07/24'
driver = webdriver.Chrome()
driver.get(url)
wait = WebDriverWait(driver, 30)
wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Year"))).click()
driver.quit()