selenium python点击

时间:2016-11-24 07:43:12

标签: python selenium

我正试图在this网站以编程方式切换国家进行某些自动化测试,每个国家/地区的价格都不同,因此我正在编写一个小工具来帮助我决定从哪里购买。

首先,我通过这样做将所有货币都列入一个列表:

def get_all_countries():
    one = WebDriverWait(driver1, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "selected-currency")))
    one.click()
    el = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "site-selector-list")))
    list_return = []
    a_tags = el.find_elements_by_tag_name('a')
    for a in a_tags:
        list_return.append(a.text)
    return list_return

例如,它返回:['United Kingdom', 'United States', 'France', 'Deutschland', 'España', 'Australia', 'Россия']然后,我遍历列表并每次调用此函数:

def set_country(text):
    is_change_currency_displayed = driver1.find_element_by_id("siteSelectorList").is_displayed()
    if not is_change_currency_displayed:  # get_all_countries function leaves dropdown open. Check if it is open before clicking it.
        one = WebDriverWait(driver1, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "selected-currency")))
        one.click()
    div = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "site-selector-list")))
    a_tags = div.find_elements_by_tag_name('a')
    for a in a_tags:
        try:
            if a.text == text:
                driver1.get(a.get_attribute("href"))
        except StaleElementReferenceException:
            set_country(text)

将a.text与文本进行比较时,我得到了一个StaleElementReferenceException,我在网上读到这意味着对象从我保存时改变了,一个简单的解决方案是再次调用该函数。但是,我不喜欢这个解决方案和这个代码很多,我认为它没有效果,需要花费太多时间,任何想法?

修改

def main(url):
    driver1.get(url)
    to_return_string = ''
    one = WebDriverWait(driver1, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "selected-currency")))
    one.click()
    el = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "site-selector-list")))
    a_tags = el.find_elements_by_tag_name('a')
    for a in a_tags:
        atext = a.text
        ahref = a.get_attribute('href')
        try:
            is_change_currency_displayed = driver1.find_element_by_id("siteSelectorList").is_displayed()
            if not is_change_currency_displayed:  # get_all_countries function leaves dropdown open.
                one = WebDriverWait(driver1, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "selected-currency")))
                one.click()
            driver1.get(ahref)
            current_price = WebDriverWait(driver1, 10).until(
                EC.visibility_of_element_located((By.CSS_SELECTOR, ".current-price")))
            to_return_string += ("In " + atext + " : " + current_price.text + ' \n')
            print("In", atext, ":", current_price.text)
        except TimeoutException:
            print("In", atext, ":", "Timed out waiting for page to load")
            to_return_string += ("In " + atext + " : " + " Timed out waiting for page to load" + ' \n')
    return to_return_string


main('http://us.asos.com/asos//prd/7011279')

2 个答案:

答案 0 :(得分:2)

如果我正确理解了问题陈述,添加break语句可以解决问题:

def set_country(text):
    is_change_currency_displayed = driver1.find_element_by_id("siteSelectorList").is_displayed()
    if not is_change_currency_displayed:  # get_all_countries function leaves dropdown open. Check if it is open before clicking it.
        one = WebDriverWait(driver1, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "selected-currency")))
        one.click()
    div = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "site-selector-list")))
    a_tags = div.find_elements_by_tag_name('a')
    for a in a_tags:
        try:
            if a.text == text:
                driver1.get(a.get_attribute("href"))
                break
        except StaleElementReferenceException:
            set_country(text)
调用DOM后,

driver.get会更新。因此,与旧页面相关的引用(即a_tags)将不起作用。

相反,you should break the loop一旦满足条件,就会在使用driver.get检索给定国家/地区页面时立即出来。因此,您可以设置所需的国家/地区,无需反复迭代以检查是否有条件,这显然会导致StaleElementReferenceException

答案 1 :(得分:0)

如果您的陈旧元素是a标记而不是div,则可以遍历标记长度并通过div获取每个元素的文本:

for i in range(len(div.find_elements_by_tag_name('a')):
    if div.find_elements_by_tag_name('a')[i].text == text:
        driver1.get(div.find_elements_by_tag_name('a')[i].get_attribute("href"))

这样你就可以获得DOM中最新的元素了。

如果您的陈旧元素是div,那么您需要在one.click()悬停或其他方式后确认下拉列表没有消失。

另一种方法是将a.text更改为等待:

wait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[StaleElementReferenceException])
a = wait.until(EC.text_to_be_present_in_element((By.YourBy)))