Python Forum

Full Version: Using Python to get attribute text
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
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.