0

I need help,

I'm trying to print the link from a website, Here is how the code looks like:

<div class="tabla_szoveg">
            <div style="cursor:pointer" onclick="konyvjelzo('1523442');" class="torrent_konyvjelzo2"></div>

I'm trying to print the number inside "konyvjelzo('1523442');"

Using selenium

Also tried:

linkgettr= driver.find_element_by_class("box_torrent_all")

but getting NONE Thanks

perzsa
  • 25
  • 5

3 Answers3

1

To print the partial value of onclick event i.e. 1523442 you have to induce WebDriverWait for the desired visibility_of_element_located() and you can use the following solution:

  • Using CSS_SELECTOR:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.tabla_szoveg>div.torrent_konyvjelzo2"))).get_attribute("onclick").split("'")[1])
    
  • Using XPATH:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[class='tabla_szoveg']/div[@class='torrent_konyvjelzo2']"))).get_attribute("onclick").split("'")[1])
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

Please check if torrent_konyvjelzo2 is dynamic ,if not then you can also replace below torrent text with it otherwise you can use below code as it is.

split("'")[1] is used to split your konyvjelzo('1523442'); text

konyvjelzo( --> item 0
1523442     --> item 1
);          --> item 2

the first item index starts from 0. so we can return 1 item.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//div[contains(@class, 'torrent')]")))
attribute=element.get_attribute("onclick")
print attribute.split("'")[1] 
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
0

i think you could just use simple regex to find this easily

ids = re.findall("onclick=\"konyvjelzo\('(.*?)'\);",page_text)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179