Home>

I'm trying to get an element in a table at a linked site, but I can't get it.
If possible, I want to get all the elements in the table and copy them to the clipboard.

Link: https://www.ebay.com/itm//303727388384

The following error message occurred while executing the corresponding code.

Error message
NoSuchElementException Traceback (most recent call last)
<ipython-input-2-99df50474d8a>in<module>
     twenty four 
     Get 25 #Brand
--->26 Brand = driver.find_element_by_xpath ('// * [@ id = "ds_div"]/div/section [3]/div/div/table/tbody/tr [1]/td [2]') .text
     27 print (Brand)
Corresponding source code

Python, Selenium

#Target URL
ebayURL = "https://www.ebay.com/itm//303727388384"
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import time
import pyperclip
options = webdriver.ChromeOptions ()
driver = webdriver.Chrome (executable_path = r "/ Applications/chromedriver", options = options)
driver.implicitly_wait (30)
#Maximize window
driver.maximize_window ()
# URL access in new tab
driver.get (ebayURL)
Get #Brand
Brand = driver.find_element_by_xpath ('// * [@ id = "ds_div"]/div/section [3]/div/div/table/tbody/tr [1]/td [2]'). Text
print (Brand)
What I tried

I tried changing driver.find_element_by_xpath to id, class, etc.
Could not get it.
Besides, I also tried beautiful soup, pandas, etc., but I want to get it
The element could not be retrieved.

Supplementary information (FW/tool version, etc.)

Google Chrome 86.0.4240.183
Python 3.6.5
selenium
jupyter notebook

  • Answer # 1

    The target element seems to be the target in the iframe element in the page.
    To refer to the page embedded in iframe in seleniumswitch_to.frame ()age
    It is necessary to target the specified frame.

    driver.get (ebayURL)
    driver.switch_to.frame (driver.find_element_by_id ('desc_ifr'))

    Also, I feel that the data in the table can be easily extracted by using Beautiful Soup.
    .page_sourceYou can get the source of the frame currently targeted by the driver with.

    from bs4 import BeautifulSoup
    soup = BeautifulSoup (driver.page_source,'html.parser')
    table = soup.find ('div', class_ ='description'). find ('table'). find ('tbody')


    After that, get the contents of tr by yourself and shape it into the desired shape by yourself.

    for tr in table.find_all ('tr'):
        title, content = [td.text for td in tr.find_all ('td')]

    The method of passing data to the clipboard is as follows.

    When using # win32clipboard (pip install pywin32)
    import win32clipboard
    def set_clipboard (txt):
        win32clipboard.OpenClipboard ()
        win32clipboard.EmptyClipboard ()
        win32clipboard.SetClipboardText (txt, win32clipboard.CF_TEXT)
        win32clipboard.CloseClipboard ()
    set_clipboard ('string you want to store in the clipboard')
    When using # pyperclip (pip install pyperclip)
    import pyperclip
    pyperclip.copy ('string you want to store in the clipboard')


    Supplement:
    With these techniques, only one element can be stored in the clipboard.
    (Data with multiple elements such as a list cannot be stored in the clipboard.)
    For example, if you want to store multiple data in the clipboard and manually paste it into Excel, etc.
    Between cells\ tWhen separating with and when starting a new line to the next line\ r \ nAnd separate it
    By formatting the data, it is possible to paste it in Excel in the correct form when pasting.
    Example:pyperclip.copy ('A1 data \ tB1 data \ t where the next line \ r \ nA2 data \ tB2 data \ r \ n \ r \ nA4 data')

    Column A Column B Column C
    A1 data B1 data Here the next line
    A2 data B2 data
    A4 data

    There is also a library for Excel, so if you want to perform the above operation,
    We also recommend using a dedicated library instead of manipulating the clipboard.