Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Python to get attribute text
#1
Hi,

I'm a beginner in Python coding and I've been trying to automate a task where a user searches for a ref no and get the corresponding text from the search results and updates it in an excel spreadsheet for against the respective ref no.

The website is https://rulings.cbp.gov/home

When we search (for ex: N306867) for a specific ref no in the homepage we get a search result page. The text I'm looking for is inside this HTML code
<div _ngcontent-c18="" fxflex="100" style="flex: 1 1 100%; box-sizing: border-box; max-width: 100%;">
Classification
</div>

So far, I've gotten the code to work where it opens and searches the ref numbers. But when I try to get that text from the category column in the result (in this case, Classification), I'm getting an error.
Can someone help me out here to get the text as a variable so that I can store it in the spreadsheet ?

Thanks, let me know if any more detail is required
Reply
#2
How we can help when no code and no traceback is provided?
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Here is the code I've gotten so far.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome(executable_path=r"C:\Python37\Scripts\chromedriver.exe")
browser.get('https://rulings.cbp.gov/home')
input_button = browser.find_element_by_xpath('//*[@id="mat-input-0"]')
input_button.send_keys('N306871')
search_button = browser.find_element_by_xpath('//*[@id="main-content"]/app-root/div/div[1]/app-header/div/div[2]/div/mat-toolbar/mat-toolbar-row[2]/div/app-search/div/div/div/mat-form-field/div/div[1]/div[2]/button')
search_button.click()
category_type = browser.find_element_by_xpath('//*[@id="main-content"]/app-root/div/div[2]/div/app-search-results/div/div/div[2]/div[1]/app-rulings-tabular-view/mat-table/mat-row/mat-cell[2]/div/div[2]').text
print(category_type)
and the output is this:
Error:
Traceback (most recent call last): File "C:\Python37\Scripts\BTI.py", line 16, in <module> category_type = browser.find_element_by_xpath('//*[@id="main-content"]/app-root/div/div[2]/div/app-search-results/div/div/div[2]/div[1]/app-rulings-tabular-view/mat-table/mat-row/mat-cell[2]/div/div[2]').text File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value'] File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main-content"]/app-root/div/div[2]/div/app-search-results/div/div/div[2]/div[1]/app-rulings-tabular-view/mat-table/mat-row/mat-cell[2]/div/div[2]"} (Session info: chrome=79.0.3945.79)

(Dec-18-2019, 11:05 AM)buran Wrote: How we can help when no code and no traceback is provided?
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Sorry. I tried to edit OP but not able to do it now. So i have replied above in a separate comment.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 3.9 : BeautifulSoup: 'NoneType' object has no attribute 'text' fudgemasterultra 1 8,907 Mar-03-2021, 09:40 AM
Last Post: Larz60+
  select all the span text with same attribute JennyYang 2 2,129 Jul-28-2020, 02:56 PM
Last Post: snippsat
  Error object has no attribute text hcyeap 3 13,908 May-21-2019, 07:12 AM
Last Post: buran
  BeautifulSoup 'NoneType' object has no attribute 'text' bmccollum 9 14,626 Sep-14-2018, 12:56 PM
Last Post: bmccollum
  Fixing - MIME type ("text/x-python") that is not "text/event-stream" mostek_6502 4 8,996 Sep-22-2017, 08:17 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020