繁体   English   中英

Python Selenium Internet explorer 找不到 css 选择器,或 Z3D788FA62D7C74105A1BEE4C91

[英]Python Selenium Internet explorer not able to find css selector, or xpath

因此,关于这方面的一些背景知识,flask 正在通过 alwaysup 运行,这使 flask 始终作为 windows 服务运行。 这整个过程使我的 selenium 脚本在不同的实例中启动,而不是在本地 VM 中。 当它在该实例中运行时,它具有相同的 Internet 设置,并且可以毫无问题地导航到 url。 例如,一旦需要开始填写表格,它就会卡住。 driver.find_element_by_id('ctl00_PlaceHolderMain_CreatePerson_uoc_BasicInfo_grouping_UserType\ _control_internalDropDownList').send_keys(Keys.DOWN)是它正在寻找的第一个元素,即使使用睡眠定时器也无法找到它。

你可以试试 xpath 吗?

driver.find_element_by_xpath('//*[contains(@id, "BasicInfo_grouping_UserType" and contains(@id, "internalDropDownList"]').send_keys(Keys.DOWN) 

如果这不起作用,可能是调整隐式等待driver.implicitly_wait(20)

或添加显式等待

elem = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, '//*[contains(@id, "BasicInfo_grouping_UserType" and contains(@id, "internalDropDownList"]')))
# OR
elem = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, 'ctl00_PlaceHolderMain_CreatePerson_uoc_BasicInfo_grouping_UserType_control_internalDropDownList')))
elem.send_keys(Keys.DOWN) 

在查找元素之前,请尝试使用 F12 开发者工具检查是否可以从页面资源中找到元素? class 名称或 id 值是否正确? 并检查您是否使用 Iframe?

如果不使用 iframe 标记,那么,您可以参考以下示例代码来查找元素(从您的代码中,似乎该元素是一个下拉列表,我想您想要 select 选项)。

import time
from selenium import webdriver
driver = webdriver.Ie("D:\\Downloads\\webdriver\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe")

# connect to the specific ip address
driver.get("<the website url>")

#print("prompt sample")
#find element by id
#driver.find_element_by_id('Text1').send_keys("hello world")   
#find element by xpath
driver.find_element_by_xpath("//input[@id='Text1']").send_keys("hi")

#execute the javascript function and set the input text value.
#driver.execute_script("document.getElementById('Text1').value = 'Hi';")

#find element by xpath and using the id attribute.
driver.find_element_by_xpath("//select[@id='ctl00_PlaceHolderMain_CreatePerson_uoc_BasicInfo_grouping_UserType_control_internalDropDownList']/option[text()='Open']").click()

#find element by xpath and using the class attribute.
driver.find_element_by_xpath("//select[@class='ddl_internalDropDownList_cs']/option[text()='In Progress']").click()

结果如下:

在此处输入图像描述

web 页面资源如下:

<input id="Text1" type="text" value="" />
<select id="ctl00_PlaceHolderMain_CreatePerson_uoc_BasicInfo_grouping_UserType_control_internalDropDownList">
    <option value="">-- None --</option>
    <option value="1">Pending</option>
    <option value="2">Queued</option>
    <option value="3">Open</option>
    <option value="4">In Progress</option>
    <option value="5">Cancelled</option>
    <option value="6">Closed Complete</option>
</select>

<select id="ddl_internalDropDownList" class="ddl_internalDropDownList_cs">
    <option value="">-- None --</option>
    <option value="1">Pending</option>
    <option value="2">Queued</option>
    <option value="3">Open</option>
    <option value="4">In Progress</option>
    <option value="5">Cancelled</option>
    <option value="6">Closed Complete</option>
</select>

如果元素位于 iframe 标签中,首先我们应该找到 iframe 元素并切换到 iframe 中,然后找到其中的元素。

您可以参考以下示例代码:

import time
from selenium import webdriver
driver = webdriver.Ie("D:\\Downloads\\webdriver\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe")

# connect to the specific ip address
driver.get("<the website url>")

#print("prompt sample")
#find the iframe tag and switch to the frame
iframe = driver.find_elements_by_tag_name('iframe')[0]
driver.switch_to_frame(iframe)

#driver.find_element_by_id('Text1').send_keys("hello world")   

driver.find_element_by_xpath("//input[@id='Text1']").send_keys("hi")

#execute the javascript function and set the input text value.
#driver.execute_script("document.getElementById('Text1').value = 'Hi';")

driver.find_element_by_xpath("//select[@id='ctl00_PlaceHolderMain_CreatePerson_uoc_BasicInfo_grouping_UserType_control_internalDropDownList']/option[text()='Open']").click()

driver.find_element_by_xpath("//select[@class='ddl_internalDropDownList_cs']/option[text()='In Progress']").click()

# move out of iframe
driver.switch_to_default_content()

结果如下:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM