硒中的selexbox当前检查错误 [英] selexbox present check error at selenium

查看:86
本文介绍了硒中的selexbox当前检查错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains


driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://motul.lubricantadvisor.com/Default.aspx?data=1&lang=ENG&lang=eng")

def getallcars():
    wait = WebDriverWait(driver, 10)
    wait.until(EC.presence_of_element_located((By.ID, "ctl00_ContentPlaceHolder1_rptCategoryBtn_ctl01_btnImage")))

    driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_rptCategoryBtn_ctl01_btnImage").click()

    wait.until(EC.presence_of_element_located((By.ID, "ctl00_ContentPlaceHolder1_lblSelectedMake")))
    driver.find_element(By.ID, 'ctl00_ContentPlaceHolder1_lblSelectedMake').click()

    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ctl00_ContentPlaceHolder1_lstMake")))

    el = driver.find_element(By.NAME,"ctl00$ContentPlaceHolder1$lstMake")
    car =[]
    for option in el.find_elements(By.TAG_NAME,'option'):
        car.append((option.text).encode('utf8'))
    return car

cars=getallcars()


for value in cars:
    drop = driver.find_element(By.CSS_SELECTOR, '#ctl00_ContentPlaceHolder1_lstMake')
    sel = Select(drop)
    sel.select_by_visible_text(value)
    time.sleep(2) #<---- THIS POINT!!
    driver.find_element(By.ID,'ctl00_ContentPlaceHolder1_HeaderModel').click()
    el2 = driver.find_element(By.NAME, "ctl00$ContentPlaceHolder1$lstModel")
    print "The models for %s are:"  %value
    for option in el2.find_elements(By.TAG_NAME,'option'):
        print  option.text
    action = ActionChains(driver)
    action.move_to_element_with_offset(el2, 300, 200)
    action.click()
    action.perform()
    driver.find_element(By.CSS_SELECTOR,'#ctl00_ContentPlaceHolder1_HeaderMake').click()

我一直在做爬虫.我还不完全了解.所以我有一个问题.也许是34行代码.我被标记为# 它是"time.sleep(2)"方法.因为更改"sel.select_by_visible_text(value)"

I have been make the crawler. I don't understand completely yet. so I have a question. maybe It's 34line at code. I was mark about # it's use be the "time.sleep(2)" method. because It didn't detect the select box when It's change about "sel.select_by_visible_text(value)"

我该怎么做?我不想使用"time.sleep(2)"方法. 我已经尝试过"expected_conditions.presence_of_element_located"它不起作用.我想这是关于保管箱的问题.这个尺寸基本上不是因为我尝试其他尺寸尝试过的expected_conditions.presence_of_element_located

how can I do that? I don't want to use the "time.sleep(2)"method. already I tried "expected_conditions.presence_of_element_located" It doesn't work. I guess It's problem about dropbox. this size is not basically because It did well when I tried another size tried "expected_conditions.presence_of_element_located"

推荐答案

显式等待将不起作用,因为您可以使用的条件是可点击的元素",可见的元素"等. 用于显式等待的元素也可用并且可以单击,但是由于其他元素与之重叠而失败.

Explicit wait will not work, because the conditions you can use are "element to be clickable", "element to be visible" and like that. The element that you using for explicit wait is available and clickable also, but its failing because other element is overlapping it.

由于另一个元素的重叠需要一段时间才能消失,因此我们必须等待重叠消失后才能单击该元素. 显式等待可以等待元素的出现和可单击,该元素已经存在但被其他元素隐藏.

Since the other element's overlap takes time to disappear, we have to wait for the overlap to disappear before we can click on the element. Explicit wait can wait for element to appear and clickable, which it is already is but it is being hidden by other element.

在这种情况下,我们必须使用time.sleep()进行艰难的等待

In this scenario, we have to use time.sleep() to put a hard wait

这篇关于硒中的selexbox当前检查错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆