-1

How do I click on multiple tags for yahoo search results using selenium. For example I search for states and multiple results show up. I want to click on each link> heres what the xpath links look like

xpath
#//*[@id="yui_3_10_0_1_1607785449630_1057"]
#//*[@id="yui_3_10_0_1_1607785449630_1057"]
#//*[@id="yui_3_10_0_1_1607785449630_1189"]

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


for i in range(1):
    sleep(2)
    driver = webdriver.Firefox()

    driver.get("http://www.yahoo.com/")
    search = driver.find_element_by_id("ybar-sbq")
    my_choice = random.choice(["Atos stock"])
    search.send_keys(my_choice)
    sleep(2)
    pg.press('enter')
    sleep(4)
    for i in range(1):
        try:
    
            #//*[@id="yui_3_10_0_1_1608697269369_685"]
            sleep(2)
            driver.find_elements_by_xpath('//*[@id^="yui_3"]')

            #driver.find_elements_by_xpath("//*[@id^='yui_3']").click()
            sleep(2)
            driver.execute_script("window.scrollBy(0, 10)")
            sleep(2)
            driver.execute_script("window.scrollBy(0, 50)")
            sleep(2)
            driver.execute_script("window.scrollBy(0, 100)")
            sleep(5)
            #driver.back()
            print('done')
            sleep(2)
        except:
            print('error')
            continue
driver.close()
driver.quit()
Jean S
  • 13
  • 5
  • Where exactly is your program hung up? It is unclear. Are you able to successfully click the links then have difficulty getting `back`? Or is your issue with getting the elements by id `yui_3^`. Maybe check this post [here](https://stackoverflow.com/questions/53398126/clicking-multiple-items-on-one-page-using-selenium) – Chris Turgeon Dec 12 '20 at 17:50
  • The issue is getting the elements by id yui_3^. The link is helpful but there's no specific class name. All the ids start with #//*[@id="yui_3_10_0_1_ thats the only repeating item. – Jean S Dec 12 '20 at 17:57
  • @JeanS This sounds like an [X-Y problem](http://xyproblem.info/). Instead of asking for help with your solution to the problem, edit your question and ask about the actual problem. What are you trying to do? – undetected Selenium Dec 12 '20 at 19:09
  • @DebanjanB just trying to get links after search results and click on them from yahoo. Yahoo has weird tags compared to bing, google or other search engines – Jean S Dec 12 '20 at 19:11
  • how do I click on href tags after getting search result. They change each tag. I want to click on them Thats all --->//*[@id="yui_3_10_0_1_1607785449630_1057"] – Jean S Dec 12 '20 at 19:13

1 Answers1

0

This should should get all elements with the starting of yui_3.

elems=driver.find_elements_by_css_selector("[id^='yui_3']")
print(len(elems))

Now what happens when you click on them may make the elements stale or not.

Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32