Home > Back-end >  Using Python Selenium to select a tag within divs of a class within a div?
Using Python Selenium to select a tag within divs of a class within a div?

Time:03-02

Alright so, what I'm trying to do is searching for the first a tags within the divs of a specific class, in a div with a specific ID. Using Python Selenium offcourse.

Right now I have as my code

newest_elements = driver.find_elements_by_css_selector("div.elements > a")

What this is doing is searching for all divs in a page with class "elements", and taking the very top most link from those divs. But I do not want to search all of the divs on the entire page with the class "elements". I only want to search the "elements" divs that are in another larger div with an specific id called "list-all".

How do I achieve this? Thanks in advance for your help guys

CodePudding user response:

According to your description instead of

newest_elements = driver.find_elements_by_css_selector("div.elements > a")

You should use

newest_elements = driver.find_elements_by_css_selector("div#list-all div.elements > a")

You may possibly add waits / delays here.

  • Related