繁体   English   中英

元素不可见。 引发TimeoutException(消息,屏幕,堆栈跟踪)selenium.common.exceptions.TimeoutException

[英]Element is not visible. raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException

有以下页面。

http://remitly.com/us/en/

您需要选择一个特定的countryclick它,但是在编制下一行时,出现错误。

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

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