1

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')]")
0

3 Answers 3

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

xpath did not work either. Sorry, I will update the question
0

Pass only partial class name like,

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

1 Comment

Put wait before finding element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.