selenium.common.exceptions.ElementNotVisibleException:消息:尝试使用Selenium Python单击元素时元素不可交互错误

时间:2019-03-28 23:45:37

标签: python selenium selenium-webdriver webdriver webdriverwait

我正在尝试使用Python Selenium通过在此页面中单击“导出到Excel”来下载Excel文件:

https://www.hkex.com.hk/Market-Data/Futures-and-Options-Prices/Equity-Index/Hang-Seng-Index-Futures-and-Options?sc_lang=en#&product=HSI

在Chrome Inspect模式下,我认为该元素的名称为“ ete”

public class Permuter {
    static void permute(List<Integer> list, int recursionNestingLevel, List<List<Integer>> results){
        for (int listIndex = recursionNestingLevel; listIndex < list.size(); listIndex++){
            Collections.swap(list, listIndex, recursionNestingLevel);
            permute(list, recursionNestingLevel + 1, results);
            Collections.swap(list, recursionNestingLevel, listIndex);
        }
        if (recursionNestingLevel == list.size() - 1){
            results.add(new ArrayList<>(list));
        }
    }
    public static void main(String[] args){
        List<Integer> listForPermutation = Arrays.asList(1,2,3,4);
        List<List<Integer>> permutations = new ArrayList<>();
        Permuter.permute(listForPermutation, 0, permutations);
        System.out.println("Permutations without null values and without duplicates:");
        System.out.println(permutations.stream().map(list -> list.toString()).collect(Collectors.joining(System.lineSeparator())));
        List<List<Integer>> permutationsWithNulls = permutations.stream().map(list -> list.stream().map(i -> i == 1 ? null : i).collect(Collectors.toList())).collect(Collectors.toList());
        System.out.println("Permutations without null values and without duplicates:");
        System.out.println(permutationsWithNulls.stream().map(list -> list.toString()).collect(Collectors.joining(System.lineSeparator())));

    }
}

这是我的代码:

<div class="ete title_right" id="ete">Export to Excel</div>

但是,运行我的代码时会返回元素不可交互的异常:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")

driver = webdriver.Chrome('/usr/local/bin/chromedriver', chrome_options=options)

driver.get("https://www.hkex.com.hk/Market-Data/Futures-and-Options-Prices/Equity-Index/Hang-Seng-Index-Futures-and-Options?sc_lang=en#&product=HSI")

driver.find_element_by_id('ete').click()

html = driver.page_source
print(html)
driver.close()

[更新]

我使用了Debanjan的方法,但是返回了TimeoutException:

selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
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
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
#options.add_argument("--disable-dev-shm-usage")

driver = webdriver.Chrome('/usr/local/bin/chromedriver', chrome_options=options)

driver.get('https://www.hkex.com.hk/Market-Data/Futures-and-Options-Prices/Equity-Index/Hang-Seng-Index-Futures-and-Options?sc_lang=en#&product=HSI')
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='textrow' and text()='FUTURES']//following::div[@class='ete title_right' and text()='Export to Excel']"))).click()

html = driver.page_source
print(html)
driver.close()

2 个答案:

答案 0 :(得分:0)

页面元素需要一些时间才能加载到该页面上,而您要访问的部分似乎最后加载了。您需要做的就是等待该元素可见,然后再尝试单击它。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

WebDriverWait(driver, 30).until(ec.visibility_of_element_located((By.CSS_SELECTOR, "#ete")))

等待之后,该元素可见,您可以单击它。

这里是一个具有良好信息的网站:http://allselenium.info/wait-for-elements-python-selenium-webdriver/

答案 1 :(得分:0)

此错误消息...

selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable

...表示您想与之交互时,该元素不可交互。

理想情况下,当您尝试click()时,应使 WebDriverWait 成为所需的 beableable 。此外,有两个(2)元素的文本为导出到Excel ,并且我考虑了文本顶部元素为 FUTURES 的页面顶部的元素您可以使用以下解决方案:

  • 代码块:

    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
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument("--disable-extensions")
    options.add_argument('disable-infobars')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://www.hkex.com.hk/Market-Data/Futures-and-Options-Prices/Equity-Index/Hang-Seng-Index-Futures-and-Options?sc_lang=en#&product=HSI')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='textrow' and text()='FUTURES']//following::div[@class='ete title_right' and text()='Export to Excel']"))).click()
    
  • 浏览器快照:

download