简体   繁体   中英

selenium css selector or xpath for complex class doesn't work when run as script

The following code, which extracts elements using css selector, works in the ipython3 terminal, but doesn't find the elements when run as script:

from selenium import webdriver 
driver = webdriver.Chrome()

url = scrape_url + "&keywords=" + keyword
driver.get(url)
driver.find_elements_by_css_selector(".search-result.search-result__occluded-item.ember-view")

The complex class of the element:

"search-result search-result__occluded-item ember-view"

The following xpath worked in the terminal, but not as a script:

driver.find_elements_by_xpath("//li[contains(@class, 'search-result search-result__occluded-item')]")

If you can't find any elements with selenium css selector, then can you always try to use xpath instead of the css selector.

More information about that can be found here .

只传递部分类名,如,

driver.find_elements_by_css_selector(".search-result__occluded-item")

This might be a timing issue: required element could be generated dynamically, so you need to wait some time until it appears in DOM :

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver 
driver = webdriver.Chrome()

url = scrape_url + "&keywords=" + keyword
driver.get(url)
wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//li[contains(@class, 'search-result search-result__occluded-item')]")))

Also some class names could be assigned dynamically. That's why using compound name as "search-result search-result__occluded-item ember-view" might not work without ExplicitWait

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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