Python/Selenium;无法在Internet Explorer中按类找到元素



我完全是个新手,尝试在python中使用selenium在Internet Explorer中单击。当我只能看到class="0"时;drop_ text2";从Internet Explorer中的inspect元素,我如何键入代码来单击它?

这是我的脚本:

driver.find_element_by_class_name('drop_text2').click()

并检查InternetExplorer:中的元素

<span class="drop_text2">Matrix</span>

你能向我解释一下我做错了什么吗?

首先,等待页面完全加载,然后可能需要执行两次单击操作才能使其工作。

这里有一个简单的演示:

<span class="drop_text2" onclick="myFunction()">Matrix</span>
<script>
function myFunction() {
window.alert('You clicked me!');
}
</script>

from selenium import webdriver
from selenium.webdriver.ie.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

ser = Service(r"C:\Users\Administrator\Desktop\IEDriverServer.exe")
driver = webdriver.Ie(service = ser)
driver.get("https://localhost:44356/Index.html")
wait_time = 60 # wait for page to load
element = WebDriverWait(driver, wait_time).until(EC.element_to_be_clickable((By.CLASS_NAME,"drop_text2")))
element.click()
element.click()

最新更新