0

This is my code for now:

from selenium import webdriver
import time
from selenium.webdriver.common.by import By


options = webdriver.EdgeOptions()


browser = webdriver.Edge(options=options)

browser.maximize_window()
browser.get("http://www.example.com")

browser.find_element(By.CSS_SELECTOR, "input[type='submit']").click()

time.sleep(3)

window_before = browser.window_handles[0]

browser.find_element(By.PARTIAL_LINK_TEXT,
                     "Details about me").click()

window_after = browser.window_handles[1]

browser.switch_to.window(window_after)

The following code:

browser.find_element(By.PARTIAL_LINK_TEXT,
                     "Details about me").click()

Opens a new window and I would like to receive the whole text for table border:

<table border="1" cellspacing="5"><tbody><tr><th>IP</th><th>Hits</th><th>Field 1<br>Times</th><th>Field 2</th></tr><tr><td>123 </td><td>25</td><td>132 15:31 </td><td>25.07.2023 17:00</td></tr> 
<tr><td>1234 </td><td>1</td><td>19.07.2023 02:38 </td><td>26.07.2023 04:00</td></tr> 
</tbody></table>

Could you please assist as I am stuck?

svaleriev
  • 13
  • 3

2 Answers2

0

If I understand your question correctly, the .click() is clicking a link and you want to find a/the table element on the site you're fetching by clicking that link, and show its HTML.

Try adding:

# Get the first <table> tagged element.
element = browser.find_element(By.TAG_NAME, "table")

# Get the outer and inner HTML (i.e. includes the <table> tag.)
html = element.get_attribute('outerHTML')

# Print HTML to output.
print(html)

after

browser.find_element(By.PARTIAL_LINK_TEXT,
                     "Details about me").click()
Trasp
  • 1,132
  • 7
  • 11
0

Given the HTML of the <table> element:

<table border="1" cellspacing="5">
    <tbody>
        <tr>
            <th>IP</th>
            <th>Hits</th>
            <th>
                Field 1
                <br>
                Times
            </th>
            <th>Field 2</th>
        </tr>
        <tr>
            <td>123 </td>
            <td>25</td>
            <td>132 15:31 </td>
            <td>25.07.2023 17:00</td>
        </tr> 
        tr>
            <td>1234 </td>
            <td>1</td>
            <td>19.07.2023 02:38 </td>
            <td>26.07.2023 04:00</td>
        </tr> 
    </tbody>
</table>

To switch to the new tab and extract the value of the <table> border attribute you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using TAG_NAME:

    handle_before = browser.current_window_handle
    browser.find_element(By.PARTIAL_LINK_TEXT, "Details about me").click()
    WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))
    handles_after = browser.window_handles
    new_window_handle = [handle for handle in handles_after if handle != handle_before][0]
    browser.switch_to.window(new_window_handle)
    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.TAG_NAME, "table"))).get_attribute("border"))
    
  • Using CSS_SELECTOR:

    handle_before = browser.current_window_handle
    browser.find_element(By.PARTIAL_LINK_TEXT, "Details about me").click()
    WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))
    handles_after = browser.window_handles
    new_window_handle = [handle for handle in handles_after if handle != handle_before][0]
    browser.switch_to.window(new_window_handle)
    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table[border]"))).get_attribute("border"))
    
  • Using XPATH:

    handle_before = browser.current_window_handle
    browser.find_element(By.PARTIAL_LINK_TEXT, "Details about me").click()
    WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))
    handles_after = browser.window_handles
    new_window_handle = [handle for handle in handles_after if handle != handle_before][0]
    browser.switch_to.window(new_window_handle)
    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[@border]"))).get_attribute("border"))
    
  • 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 Python Selenium - get href value

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