metadog metadog - 1 year ago 90
CSS Question

Selenium Python - Finding div containing two specific elements

I'm building a python script using selenium, and have ran into a quite confusing problem.

The website lists products using a name, which is not unique, and a color, which is not unique either. The color and name elements have the same parent.

My script gets user input on which product he wants the script to buy for him, and which color.

The problem:
I can't for the life of me figure out how to select the right product using the two variables productName and productColor.


<div class="inner-article">
<a class="product-name">Silk Shirt</a>
<a class="product-color">Black</a>

What i've tried so far:
Obviously, selecting the first product named Silk Shirt on the page is quite easy. I considered selecting the first product, then selecting that products parent, selecting that elements parent, then selecting that parents second child, checking if it was black, and proceeding, but CSS doesn't have a parent selector.

How would i go about doing this?

Answer Source

Create a main loop that selects each div class="inner-article" element.

In the loop, look for elements that have an h1 child element and an a class=product-name grandchild element with text of "Silk Shirt", and a p child element and a a class=product-color grandchild element with text of "Black".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download