What is the best way to avoid NoSuchElementException in Selenium?

后端 未结 9 753
时光说笑
时光说笑 2020-11-27 18:31

I have written few test cases in Selenium WebDriver using Java and execute them on grid (hub and multiple nodes). I have noticed that a few test cases fail due to NoSu

9条回答
  •  孤独总比滥情好
    2020-11-27 19:04

    Sometimes it is possible to wait for the download of the desired item.

    driver.get("https://zzzzzzzzz.market/items/mirage_prime_set")
    
    WebDriverWait(driver, 20)
           .until(
            EC.visibility_of_element_located(
              (By.XPATH, ('//div[@class="orders-row__element order__price sell_color"]')
            )))
    

    Sometimes you need to do something so that the UI framework loads the data. For example scroll page

    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    

    And then get the necessary data

    responsetext=driver.page_source
    
    from lxml import html
    parsed_body = html.fromstring(responsetext)
    
    obj1 = parsed_body.xpath('.//div[@class="orders-row__element order__price sell_color"]/span[1]')
    print(len(obj1))
    

提交回复
热议问题