简体   繁体   English

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

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

So a little background on this, flask is being run through alwaysup, which keeps flask always up as a windows service.因此,关于这方面的一些背景知识,flask 正在通过 alwaysup 运行,这使 flask 始终作为 windows 服务运行。 This whole process makes my selenium script start in a different instance not in the local VM.这整个过程使我的 selenium 脚本在不同的实例中启动,而不是在本地 VM 中。 When it's running in that instance, it has the same internet settings and it navigates to the url without any problem.当它在该实例中运行时,它具有相同的 Internet 设置,并且可以毫无问题地导航到 url。 It gets stuck once it needs to start filling out the form, for example.例如,一旦需要开始填写表格,它就会卡住。 driver.find_element_by_id('ctl00_PlaceHolderMain_CreatePerson_uoc_BasicInfo_grouping_UserType\ _control_internalDropDownList').send_keys(Keys.DOWN) is the first element it's looking for and it's unable to find it, even with a sleep timer. driver.find_element_by_id('ctl00_PlaceHolderMain_CreatePerson_uoc_BasicInfo_grouping_UserType\ _control_internalDropDownList').send_keys(Keys.DOWN)是它正在寻找的第一个元素,即使使用睡眠定时器也无法找到它。

can you try with an xpath?你可以试试 xpath 吗?

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

If that doesnt work, may be adjust Implicit Wait driver.implicitly_wait(20)如果这不起作用,可能是调整隐式等待driver.implicitly_wait(20)

or Add Explicit Waits或添加显式等待

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) 

Before finding the elements, please try to use F12 developer tools to check whether you could find the elements from the page resource?在查找元素之前,请尝试使用 F12 开发者工具检查是否可以从页面资源中找到元素? whether the class name or id value is correct? class 名称或 id 值是否正确? And check if you are using Iframe or not?并检查您是否使用 Iframe?

If not using iframe tag , then, you could refer to the following sample code to find the elements (from your code, it seems that the element is a dropdownlist, I suppose you want to select the options).如果不使用 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()

The result as below:结果如下:

在此处输入图像描述

The web page resource like this: 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>

If the element located in the iframe tag , at first, we should find the iframe element and switch to the iframe, then, find elements inside it.如果元素位于 iframe 标签中,首先我们应该找到 iframe 元素并切换到 iframe 中,然后找到其中的元素。

you could refer to the following sample code:您可以参考以下示例代码:

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()

The result as below:结果如下:

在此处输入图像描述

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

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