Python Selenium Internet Explorer无法找到CSS选择器或XPath [英] Python Selenium Internet explorer not able to find css selector, or xpath

查看:75
本文介绍了Python Selenium Internet Explorer无法找到CSS选择器或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)是它要查找的第一个元素,即使有睡眠计时器也找不到.

So a little background on this, flask is being run through alwaysup, which keeps flask always up as a windows service. This whole process makes my selenium script start in a different instance not in the local VM. When it's running in that instance, it has the same internet settings and it navigates to the url without any problem. 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.

推荐答案

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

Before finding the elements, please try to use F12 developer tools to check whether you could find the elements from the page resource? whether the class name or id value is correct? 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).

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

结果如下:

这样的网页资源:

<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,然后在其中找到元素.

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.

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

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

结果如下:

这篇关于Python Selenium Internet Explorer无法找到CSS选择器或XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆