Home > Software engineering >  How to use CSS selector with strings
How to use CSS selector with strings

Time:03-18

I've been trying to create a program that finds data on a site using selenium. I've downloaded the webdriver and by module.

driver.get("https://en.wikipedia.org/wiki/Main_Page")
number = driver.find_element(By.CSS_SELECTOR("articlecount a"))
print(number)

When I run the code, it displays the error that the str object is not callable. (I'm assuming the error comes from the article count a part.

I've tried removing the two quotations around "articlecount a" but that just creates more errors.

Does anyone know what I have to do to allow the CSS Selector to extract data?

CodePudding user response:

Instead of this

number = driver.find_element(By.CSS_SELECTOR("articlecount a"))

It should be like

number = driver.find_element(By.CSS_SELECTOR,"articlecount a")
print(number)

CodePudding user response:

There are several problems here:

  1. The command you are trying to use has somewhat different syntax.
    Instead of
number = driver.find_element(By.CSS_SELECTOR("articlecount a"))

It should be something like

number = driver.find_element(By.CSS_SELECTOR,"articlecount a")
  1. Your locator is wrong.
    Instead of articlecount a it probably should be #articlecount a
  2. You are missing a delay here. The best practice is to use Expected Conditions explicit waits
  3. You need to extract the text value from number web element object.

With all the above your code will be:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)
driver.get("https://en.wikipedia.org/wiki/Main_Page")
number = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#articlecount a"))).text
print(number)

The output here will be

6,469,178
  • Related