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

喜你入骨 提交于 2021-01-29 12:00:34

问题


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.


回答1:


can you try with an 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)

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) 



回答2:


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?

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

The result as below:

The web page resource like this:

<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.

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:



来源:https://stackoverflow.com/questions/58596564/python-selenium-internet-explorer-not-able-to-find-css-selector-or-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!