简体   繁体   中英

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

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) 

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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