Home > Blockchain >  Best practice python syntax using Selenium
Best practice python syntax using Selenium

Time:04-12

First time ever posting here, please be gentle, not even sure I have all the terminology correct. I couldn't find anything related.

I'm trying to add a find_element_by_css_selector, and getting a deprecated warning. I'd like to use the proper syntax for upcoming versions (find_element(By.Css_SELECTOR)) however 'By.' isn't recognized as a valid call?

I've tried using the syntax suggested from the terminal log (by=By.CSS_SELECTOR) in which case 'by=' is recognized, but running that returns NameError: name 'By.' is not defined

elem_list= browser.find_element(by=By.CSS_SELECTOR("div.a-section.a-spacing-medium._octopus-search-result-card_style_apbSearchResultsContainer__bCqjb"))

I've also tried

elem_list= browser.find_element(By.CSS_SEECTOR, "div.a-section.a-spacing-medium._octopus-search-result-card_style_apbSearchResultsContainer__bCqjb")

I tried updating Selenium to see if I was missing something and got the same errors both times.

Again, this is all pretty new to me and I created a StackExchange account just to try and figure this out.

CodePudding user response:

You have to add the import statements below:

from selenium.webdriver.common.by import By

If my answer helps you accept my answer and also upvote

CodePudding user response:

first you need to import By from selenium.webdriver.common.by

from selenium.webdriver.common.by import By

Here is how to implement By in code

elem_list = browser.find_element(By.CSS_SELECTOR(
    "div.a-section.a-spacing-medium._octopus-search-result-card_style_apbSearchResultsContainer__bCqjb"))
  • Related