0

how can i select Armen Khl

using python with selenium

<table id="ads-link-1091937" class="work-serf" data-status="inactive">
 <tbody>
  <tr class="ads_1091937">
    <td width="18">
       <div class="ybprosm" title="Просмотр видео"></div>
    </td>
    <td>
       <div id="start-ads-1091937">
         <span onclick="if (!window.__cfRLUnblockHandlers) return false; funcjs['start_youtube'](1091937, '0106019a4aaa39706c917c8bce23c111', 'ads-start', 'ads');" title="https://www.youtube.com/watch?v=LltHv-MPp_E" style="cursor:pointer;color:#006699;" data-cf-modified-2376def8386a7bac71794558-="">Armen Khl</span>          <br>
         <span class="serf-text">Просмотр видеоролика</span>

view_text = driver.find_element("xpath", '//*[@class="work-serf"]'/tbody/tr/td[2]/div/span)

plz tell me what i wrong in this line code

nohack
  • 1
  • What's the error? – KunduK Jun 06 '23 at 14:40
  • Welcome to [so]. This is not a code-writing or tutoring service. It is not possible to provide a specific answer without you providing sufficient information to understand your problem. Please see: [Why is Can someone help me? not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) for more details. – itprorh66 Jun 06 '23 at 15:04

1 Answers1

0

To print the text Armen Khl you can use either of the following locator strategies:

  • Using css_selector and get_attribute("innerHTML"):

    print(driver.find_element(By.CSS_SELECTOR, "table.work-serf > tbody > tr td > div > span[onclick][title]").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element(By.XPATH, "//table[@class="work-serf"]/tbody/tr//td/div/span[@onclick and @title]").text)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.common.by import By
    

Ideally to extract the text Armen Khl you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table.work-serf > tbody > tr td > div > span[onclick][title]"))).text)
    
  • Using XPATH and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[@class="work-serf"]/tbody/tr//td/div/span[@onclick and @title]"))).get_attribute("innerHTML"))
    
  • Note : You have to add the following imports :

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

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352