selexbox present check error at selenium

耗尽温柔 提交于 2019-12-11 06:23:38

问题


Code at Selenium by python

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()

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)"

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"


回答1:


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.

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



来源:https://stackoverflow.com/questions/40601388/selexbox-present-check-error-at-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!