0
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//[@id="dataviscontainer"]/div/iframe')))
span_texts = [item.text for item in driver.find_elements_by_css_selector('span')] 
print(span_texts)

My objective here is to scrape text from multiple span tags which actually are in dropdown list which is inside a iframe & the iframe does not have any name class or id so I used the XPath. After running this code I get an empty list. below are 2 span tags that has the text

<span class="slicerText" title="Albury (C)" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); border-style: solid; border-color: rgb(96, 94, 92); border-width: 0px; font-size: 13.3333px; font-family: Arial; line-height: 17px;">Albury (C)</span>
<span class="slicerText" title="Armidale Regional (A)" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); border-style: solid; border-color: rgb(96, 94, 92); border-width: 0px; font-size: 13.3333px; font-family: Arial; line-height: 17px;">Armidale Regional (A)</span>
bambi1910
  • 29
  • 8

3 Answers3

0

You should first select the iframe element in a variable then apply the selection of the spans on the variable that contains the iframe reference.

this answer should help you.

OmG3r
  • 1,658
  • 13
  • 25
  • I did check that answer I do not want to add text i want to scrape that text everytime I try that it gives me empty list – bambi1910 May 18 '21 at 17:21
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"XPATH","selector":"/html/body/div[6]/div[1]/div/div[2]/div/div[1]/div/div/div[2]/div/span"} <==== getting this error that this XPATH or span class does not exist – bambi1910 May 18 '21 at 17:29
0
var iframe = driver.findElements(By.tagName("iframe"))
driver.switchTo().frame(iframe)


WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//[@id="dataviscontainer"]/div/iframe')))
span_texts = [item.text for item in
driver.find_elements_by_css_selector('span')] 
print(span_texts)

)

0

Are you looking for something like this:

spanText = driver.find_elements_by_xpath(".//span[@class='slicerText']")
for e in spanText:
    print(e.get_attribute("title"))
itronic1990
  • 1,231
  • 2
  • 4
  • 18