我有一个过时的元素参考,但在我的情况下不知道如何解决

发布于 2025-01-23 17:02:56 字数 4017 浏览 1 评论 0原文

所以我有问题。我正在尝试使用Python Selenium自动化一部分软件测试,但是现在我已经陷入了一些问题。我有以下代码。

在选择下拉(country_list_element = select(driver.find_element)(by.id,'cournitylst'))之后),然后有一个Ajax覆盖层。 之后,我需要选择另一个下拉列表(id_type_element = select(driver.find_element(by.id,'c6')))为“ passport号码”。 现在存在于HTML中,但是当我运行此操作时,我会收到一个错误,

Message: stale element reference: element is not attached to the page document

我的XPath具有Ajax覆盖的XPATH,并将其存储在变量“ Loading_Element_XPath”中,并试图等待它消失,然后继续进行。

我在这里做错了什么?我只需要能够选择下拉值即可。

底部HTML附加了HTML

import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

### Variables ###
PATH = 'C:\Selenium_Drivers\chromedriver.exe'
ENROLMENT_URL = **hidden for privacy**
SHORT_TIMEOUT  = 5   # give enough time for the loading element to appear
LONG_TIMEOUT = 30  # give enough time for loading to finish
LOADING_ELEMENT_XPATH = '/html/body/div[7]'

first_name = 'PyLenium'
last_name = 'Test1'
cell_number = '0790000000'
email_address = f'{first_name.casefold()}.{last_name.casefold()}@gmail.com'.replace(' ', '')
country_list = '1'
# id_number = 
passport_number = f'{first_name.casefold()}.{last_name.casefold()}!'.replace(' ', '')
password = 'Password1!'


driver = webdriver.Chrome(PATH)

driver.get(ENROLMENT_URL)                               # Navigate to the requested URL
driver.maximize_window()
first_name_element = driver.find_element(By.ID, 'c1')
first_name_element.send_keys(first_name)
last_name_element = driver.find_element(By.ID, 'c2')
last_name_element.send_keys(last_name)
cell_number_element = driver.find_element(By.ID, 'cell')
cell_number_element.send_keys(cell_number)
email_address_element = driver.find_element(By.ID, 'c3')
email_address_element.send_keys(email_address)
country_list_element = Select(driver.find_element(By.ID, 'countryLst'))
country_list_element.select_by_visible_text('South Africa')
id_type_element = Select(driver.find_element(By.ID, 'c6'))

WebDriverWait(driver, 30).until(EC.invisibility_of_element_located((By.XPATH,LOADING_ELEMENT_XPATH)))

id_type_element.select_by_visible_text('Passport Number')

time.sleep(10)

我在页面的 。麻烦的下拉菜单是'id =“ c6”'的菜单。请注意:

  • 当一个人在'id =“ cournitylst”中选择“南非”的下拉值时,'id =“ c6”下拉'下拉''下拉''下拉'''''''''''''下拉''''''''''''''''下拉会变化,但是我添加的html是在南方之后选择非洲。
  • “陈旧元素”的下拉菜单
  • 此下拉菜单('id =“ c6”)是我想做的
,是选择“乡村”作为南非,然后选择C6作为护照号码
<div class="col-md-4">
    <div class="input-group" style="margin-bottom: 1em;">
        <span class="input-group-addon "><i class="fa fa-globe" aria-hidden="true" style="font-size: 21px;"></i></span>
</div>
    <span id="countryLst_ctl">
        <select name="countryLst" id="countryLst" class="listbox fullWidth" size="1">
            <option value="0">Country of Origin</option>
            <option value="1">South Africa</option>
            <option value="2">United Arab Emirates</option>
        </select>
    </span>                        
</div>
<div class="col-md-4">
    <div class="input-group" style="margin-bottom: 1em;">
        <span class="input-group-addon">
            <img src="/assets/images/id-card.png" style="width: 18px;">
        </span>
        <span id="c6_ctl">
            <select name="c6" id="c6" class="listbox fullWidth" size="1">
                <option value="0">--Select ID Type--</option>
                <option value="1">RSA ID Number</option>
                <option value="2">Passport Number</option>
            </select>
        </span>                      
    </div>
</div>

So I am having a problem. I am trying to automate part of our software testing with Python Selenium, however now I have fallen into a bit of a problem. I have the code below.

After the selection of a drop down (country_list_element = Select(driver.find_element(By.ID, 'countryLst'))) to South Africa, then there is an ajax overlay.
After this, I need to select another drop down, (id_type_element = Select(driver.find_element(By.ID, 'c6'))) to 'Passport Number'.
Now this exists in HTML, however when I run this, I get an error

Message: stale element reference: element is not attached to the page document

I have the XPATH of the ajax overlay and stored it into a variable "LOADING_ELEMENT_XPATH" and am trying to wait for it to go away, and then carry on.

What am I doing wrong here? I just need to be able to select the drop down value.

I attached the HTML at the bottom

import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

### Variables ###
PATH = 'C:\Selenium_Drivers\chromedriver.exe'
ENROLMENT_URL = **hidden for privacy**
SHORT_TIMEOUT  = 5   # give enough time for the loading element to appear
LONG_TIMEOUT = 30  # give enough time for loading to finish
LOADING_ELEMENT_XPATH = '/html/body/div[7]'

first_name = 'PyLenium'
last_name = 'Test1'
cell_number = '0790000000'
email_address = f'{first_name.casefold()}.{last_name.casefold()}@gmail.com'.replace(' ', '')
country_list = '1'
# id_number = 
passport_number = f'{first_name.casefold()}.{last_name.casefold()}!'.replace(' ', '')
password = 'Password1!'


driver = webdriver.Chrome(PATH)

driver.get(ENROLMENT_URL)                               # Navigate to the requested URL
driver.maximize_window()
first_name_element = driver.find_element(By.ID, 'c1')
first_name_element.send_keys(first_name)
last_name_element = driver.find_element(By.ID, 'c2')
last_name_element.send_keys(last_name)
cell_number_element = driver.find_element(By.ID, 'cell')
cell_number_element.send_keys(cell_number)
email_address_element = driver.find_element(By.ID, 'c3')
email_address_element.send_keys(email_address)
country_list_element = Select(driver.find_element(By.ID, 'countryLst'))
country_list_element.select_by_visible_text('South Africa')
id_type_element = Select(driver.find_element(By.ID, 'c6'))

WebDriverWait(driver, 30).until(EC.invisibility_of_element_located((By.XPATH,LOADING_ELEMENT_XPATH)))

id_type_element.select_by_visible_text('Passport Number')

time.sleep(10)

HTML of the page is below. The troublesome drop down menu is the one with 'id="c6"'. Note about this:

  • when one selects the drop down value of "South Africa" in the 'id="countryLst"' drop down, the 'id="c6"' drop down dynamically changes, however the HTML that I added is after South Africa is selected.
  • this drop down ('id="c6"') is the one that has a "stale element"
  • What I want to be doing, is select countryLst as South Africa, and then select c6 as Passport Number
<div class="col-md-4">
    <div class="input-group" style="margin-bottom: 1em;">
        <span class="input-group-addon "><i class="fa fa-globe" aria-hidden="true" style="font-size: 21px;"></i></span>
</div>
    <span id="countryLst_ctl">
        <select name="countryLst" id="countryLst" class="listbox fullWidth" size="1">
            <option value="0">Country of Origin</option>
            <option value="1">South Africa</option>
            <option value="2">United Arab Emirates</option>
        </select>
    </span>                        
</div>
<div class="col-md-4">
    <div class="input-group" style="margin-bottom: 1em;">
        <span class="input-group-addon">
            <img src="/assets/images/id-card.png" style="width: 18px;">
        </span>
        <span id="c6_ctl">
            <select name="c6" id="c6" class="listbox fullWidth" size="1">
                <option value="0">--Select ID Type--</option>
                <option value="1">RSA ID Number</option>
                <option value="2">Passport Number</option>
            </select>
        </span>                      
    </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文