0

Hi I'm trying to select the name "Saleem" from the HTML. It is inside a table, but I don't know how if that is relevant. The name might not always be there, so I'm trying to find a way to select the element if the name is included in the table. For example "Liam" does not appear when searched, but "saleem" does. How do I click the link that appears when saleem is searched? For some reason, Selenium can't find the element with the code I wrote below.

Here is the website (I just put Saleem in the name category and searched): https://sanctionssearch.ofac.treas.gov/default.aspx

I tried the code below, but unfortunately does not work.

driver.find_element_by_id("btnDetails").click()

 <a> id="btnDetails" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$gvSearchResults$ctl02$btnDetails&quot;, &quot;&quot;, false, &quot;&quot;, &quot;Details.aspx?id=5839&quot;, false, true))" style="color:Blue">AL-IFRI, Saleem </a>

Any help is appreciated!

2 Answers 2

1

Yes, you can use it inside try..except

from selenium.common.exceptions import NoSuchElementException

# YOUR CODE 
        
try:
  webdriver.find_element_by_id('btnDetails')
except NoSuchElementException:
  # Element does not exist
else:
  # Element exists
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, but how do I select the element? I've tried select by id, but it can't find the element?
it still works in 2022
0

This will select the search results from the name saleem and take you to Saleem's page. You can do what you want to do on that page then. To come back to the search results just use browser.back()

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser_options = Options()
browser_options.add_argument('--user-agent="Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36"')
browser_options.add_argument('start-maximized')
browser = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe',options=browser_options)
browser.get('https://sanctionssearch.ofac.treas.gov/default.aspx')
search_box = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ctl00_MainContent_txtLastName"]')))
search_box.send_keys('Saleem')
search_box.send_keys(Keys.ENTER)
time.sleep(4)
try:
    results = browser.find_element_by_xpath('//*[@id="gvSearchResults"]/tbody').find_elements_by_tag_name('td')
    for result in results:
        button = result.find_element_by_tag_name('a')
        button.click()
        #Do Something
        browser.back(()
        browser.refresh()  

1 Comment

If the answer helped please accept it by clicking the grey tick under the vote sign. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.