Selenium in Python - open every link within a drop down menu
<p>I'm new to Python, but I've been searching for the past hour about how to do this and this code almost works. I need to open up every category on a collapsing (dropdown) menu, and then Ctrl+t every link within that now .active class.  The browser opens and all the categories open as well, but I'm not getting any of the .active links being opened in new tabs.  I would appreciate any help.  </p>

<pre><code>from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("pioneerdoctor.com/productpage.cfm")

cat = driver.find_elements_by_css_selector("a[href*='Product_home']")

for i in cat:
    i.click()
    child = driver.find_elements_by_css_selector("li.active &gt; a[href*='ProductPage']")
    for m in child:
        m.send_keys(Keys.CONTROL + 't')
</code></pre>

<hr>

<p>EDIT:</p>

<p>Here's the current workaround I got going by writing to a text file and using webbrowser.  The only issue I'm seeing is that it's writing duplicates of the results multiple times.  I'll be looking through the comments later to see if I can get it working a better way (which I'm sure there is).</p>

<pre><code>from selenium import webdriver
import webbrowser

print("Opening Google Chrome..")
driver = webdriver.Chrome()
driver.get("http://pioneerdoctor.com/productpage.cfm")
driver.implicitly_wait(.5)
driver.maximize_window()

cat = driver.find_elements_by_css_selector("a[href*='Product_home']")

print("Writing URLS to file..")
for i in cat:
    i.click()
    child = driver.find_elements_by_css_selector("a[href*='ProductPage']")

    for i in child:
        child = i.get_attribute("href")
        file = open("Output.txt", "a")
        file.write(str(child) + '\n')
        file.close()

driver.quit
file = open("Output.txt", "r")

Loop = input("Loop Number,  Enter 0 to quit:  ")
Loop = int(Loop)
x = 0

if Loop == 0:
    print("Quitting..")
else:
    for z in file:
        if x == Loop:
            break
            print("Done.\n")
        else:
            webbrowser.open_new_tab(z)
            x += 1
</code></pre>

<p>None of the links in those categories are not found because the css selector for the links is incorrect. Remove the <code>&gt;</code> in <code>li.active &gt; a[href*='ProductPage']</code>. Why ? <code>p &gt; q</code> gives you the immediate children of p. Space or "p q" gives you all the "q" inside p. The links you are interested in are NOT the immediate children of li. They are inside a UL which is inside the li.</p>

<p>The other problem is the way you open links in new tabs. Use this code instead:</p>

<pre><code>combo = Keys.chord(Keys.CONTROL, Keys.RETURN)
m.sendKeys(combo)
</code></pre>

<p>Thats how I do it in Java. I think that python should have Keys.chord. If I were you, then I would open the links in another browser instance. I have seen that switching between tabs and windows is not supported well by selenium itself. Bad things can happen.</p>

<p>Before you try any tabbing, make a simple example to open a new tab and switch back to the previous tab. Do the back and forth 3-4 times. Does it work smoothly ? Good. Then, do that with 3-5 tabs. Tell me how was your experience.</p>

python selenium