Select option from javascript span with python and selenium
<p>I'm trying to generate a bot to automatically make orders from walmart, but it seems like I just can't get selected the color and the quantity, since they are not really selectors and they have no id.
I'm working with python and selenium.</p>

<p>The item using for tests right now is this one:
<a href="https://www.walmart.com/ip/8-x10-Picture-Frames-Set-of-6/10404226" rel="nofollow">https://www.walmart.com/ip/8-x10-Picture-Frames-Set-of-6/10404226</a></p>

<p>About the quantity I think I was able to select the list with:</p>

<pre><code>quantity = walmart.find_element_by_css_selector("span.product-quantity-dropdown-wrapper")
</code></pre>

<p>but after that I just can't select the quantity.</p>

<p>As far as <code>quantity</code> goes, you can operate <em>the actual <code>select</code> element</em> (via the <a href="http://selenium-python.readthedocs.io/api.html#selenium.webdriver.support.select.Select" rel="nofollow"><code>Select</code> class</a>) which is not visually detected, but is actually visible. As for the color, you can <em>dynamically construct an XPath expression</em> to locate the color "by name". </p>

<p>Complete working implementation (selects white color and quantity = 2):</p>

<pre><code>import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver


driver = webdriver.Chrome("/usr/local/bin/chromedriver")
driver.maximize_window()
driver.get("https://www.walmart.com/ip/8-x10-Picture-Frames-Set-of-6/10404226")

color = "White"
quantity = 2

wait = WebDriverWait(driver, 10)

# select color
color_selector = wait.until(EC.visibility_of_element_located((By.XPATH, "//span[@class = 'variant-swatch' and @title='{color}']/..".format(color=color))))
color_selector.click()

# harcode delay - see if you can avoid it
time.sleep(0.5)

# select quantity
quantity_selector = wait.until(EC.visibility_of_element_located((By.ID, "WMItemQtyDropDown")))
quantity_selector = Select(quantity_selector)
quantity_selector.select_by_value(str(quantity))
</code></pre>

<p>That said, make sure you are not violating any Terms of Use and staying on the legal side. Make sure to explore the <a href="https://developer.walmartlabs.com/" rel="nofollow">Walmart API</a> as well (see @MattDMo's comment).</p>

python selenium