2

While trying to use the data I get from one tab in the other tab with selenium, it resets the data when the tab changes, so I can't use the data. How can I prevent this?

My Code :

for link in linkler:
driver.get(link)
oyunAdi = driver.find_element_by_xpath("//h1[@itemprop='name']").text
oyunFiyat = driver.find_element_by_xpath("//*[@class='table-prices-current']//td[3]").text
try:
    community = driver.find_element_by_xpath("//a[@id='tab-communityitems']").click()
    sleep(1)
    isimler = driver.find_elements_by_xpath("//div[@id='item-class-2']//div//div[@class='community-item']")
    devam = "a"
except:
    devam = "b"
    driver.switch_to.window(driver.window_handles[0])
    a = 0
if devam == "a":
    for isim in isimler:
        driver.switch_to.window(driver.window_handles[1])
        sleep(0.5)
        arama = driver.find_element_by_xpath("//input[@id='findItemsSearchBox']")
        arama.clear()
        arama.send_keys(oyunAdi, isim.text)
        sleep(3)
        esyaIsimler = driver.find_elements_by_xpath("//*[@class='market_listing_item_name']")
        fiyatlar = driver.find_elements_by_xpath("//*[@class='normal_price']")
        for x in range(0,len(esyaIsimler)):
            if esyaIsimler[x].text == isim:
                print(oyunAdi, oyunFiyat, esyaIsimler[x].text, fiyatlar[x].text)
    driver.switch_to.window(driver.window_handles[0])

For example, the output of the code below is 10.

isimler = driver.find_elements_by_xpath("//div[@id='item-class-2']//div//div[@class='community-item']")
print(len(isimler))

but this code gives error

driver.switch_to.window(driver.window_handles[1])
print(len(isimler))

I want to use the data of the isimler variable that I got from the previous tab in the new tab.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Emre Oz
  • 71
  • 1
  • 8

1 Answers1

0

The elements within the list isimler is from the parent tab and are associated with the parent window_handle.

As you switch window_handles using driver.switch_to.window(driver.window_handles[1]) and focus to the adjascent tab Selenium looses focus on the parent window_handle.

As Selenium doesn't have focus on the parent tab, hence print(len(isimler)) shows error.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I'm already aware of this, if possible, I want not to loose the variable after the focus is gone – Emre Oz Dec 23 '21 at 20:56
  • @EmreOz How can you even think about looking for elements of one [DOM Tree](https://javascript.info/dom-nodes) within a all together different [HTML DOM](https://www.w3schools.com/js/js_htmldom.asp)? – undetected Selenium Dec 23 '21 at 21:01