![](/img/trans.png)
[英]TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
[英]Element is not visible. raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException
有以下頁面。
您需要選擇一個特定的country
, click
它,但是在編制下一行時,出現錯誤。
引發TimeoutException(消息,屏幕,堆棧跟蹤)selenium.common.exceptions.TimeoutException
select = driver.find_element_by_class_name('f1wrnyr7')
select.click()
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")))
element.click()
為了獲得最佳效果,您必須滾動到頁面底部,然后單擊選擇國家/地區,然后單擊您想要的國家/地區,請嘗試以下操作:
#scroll to bottom page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
#wait and click country selection, update locator
elmt = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='selectButton_f1lu1q03']")))
elmt.click()
#wait and click you country want
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")))
element.click()
因為您的代碼能夠在DOM中找到元素,但是該元素在頁面上不可見,所以引發了異常。 此外,將父div
中的span
包含稱為屬性hidden
。 以下是DOM結構
<div hidden="">
<div class="f1g5w0oh">
<div class="rm-container">
<div class="rm-row">
<div class="rm-col-sm-12 order-sm-last">
<div class="f1o6pohl">
<h5 class="foyw123">Send Money To</h5>
<div class="rm-row fywghj7">
<div class="rm-col-md-6 rm-col-lg-4 fhdzg5g">
<div class="rm-col-md-6 rm-col-lg-4 fhdzg5g">
<div class="rm-col-md-6 rm-col-lg-4 fhdzg5g">
<div class="rm-col-md-6 rm-col-lg-4 fhdzg5g">
<div class="rm-col-md-6 rm-col-lg-4 fhdzg5g">
<div class="rm-col-md-6 rm-col-lg-4 fhdzg5g">
<div class="rm-col-md-6 rm-col-lg-4 fhdzg5g">
<div>
<a class="f12qs1j9" href="/us/en/colombia">
<span>
<img class="md_flag_ffypto0" src="https://media.remitly.io/COL_32x21@2x-471f08f81b303eb2d3ac61da0909673f.png" alt="Colombia"/>
<span class="md_countryName_fdxiah8">Colombia</span>
</span>
最好的方法是手動復制步驟,並了解使元素可見所需的步驟順序。
定位器最好在下面使用。 選擇國家(地區)下拉菜單。 有時也可以從國家/地區下拉列表中進行選擇,這就是為什么您可以在下面找到代碼以獲取最新的下拉列表的原因。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
# get all dropdown elements
select_a_countries = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "img[alt='Open']")))
# filter by visibility
select_a_countries = list(filter(lambda x: x.is_displayed(), select_a_countries))
# we need last one
country_to = select_a_countries[-1]
country_to.click()
# get country we need by alt attribute, should be parameterized
country = driver.find_element_by_css_selector("img[alt='Mexico']")
# scroll to and click
# same as JavaScript: driver.execute_script("arguments[0].scrollIntoView(true);", country)
country.location_once_scrolled_into_view
country.click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.