1

The screenshot of inspect code of this web page. I just want to make a dynamic click on button "search" but I tried a lot of ways and there are always problems..new to web scraping

I have tried:

from selenium import webdriver

my_url = 'https://sbs.naic.org/solar-external-lookup/lookup?jurisdiction=AL&searchType=Company&companyStatus=AC'

driver = webdriver.Chrome('D:/chromedriver')

driver.get(my_url)
driver.find_element_by_css_selector(".btn.btn-primary").click()
driver.find_element_by_xpath("//div[@class='btn btn-primary' and @id='submitBtn']").click()
driver.find_element_by_css_selector(".btn.btn-primary").click()

I just hope my pages can click the "search" button and go to the next page.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Olly
  • 35
  • 1
  • 5
  • Please check if an iframe is present in the html structure above the element – Sameer Arora Mar 31 '19 at 08:26
  • @SameerArora Hi,Thanks for the advice, I am new to web scrap and I think it has iframe before it, I copy part of it:
    – Olly Mar 31 '19 at 20:45
  • I have solved my problem now( element = WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, "//button[@class = 'btn btn-primary']")))) thanks anyone!!! – Olly Mar 31 '19 at 23:21

3 Answers3

0

To handle dynamic element use WebDriverWait and then click on the element.

WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH,'//button[@class="btn btn-primary" and @id="submitBtn"]'))).click()

Please use following imports to execute above code.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Let me know if this helps.

KunduK
  • 32,888
  • 5
  • 17
  • 41
0

Unable to access the website. However the element with text as Search seems to be Angular element so to click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.btn-group[aria-label='Search']>button.btn.btn-primary#submitBtn"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='btn-group' and @aria-label='Search']/button[@class='btn btn-primary' and @id='submitBtn']"))).click()
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • made it! and can I have ask you another question? It's about select the maximum rows, I use "driver.find_element_by_xpath("//select[@formcontrolname='rowLimitField']/option[value= '50']").click() " but it shows unable to locate element and I can make sure the pathway it's right and there seems no iframe( I tried), it use the same link with search "https://sbs.naic.org/solar-external-lookup/lookup?jurisdiction=AL&searchType=Company&companyStatus=AC" – Olly Apr 01 '19 at 01:38
  • – Olly Apr 01 '19 at 01:45
  • @Olly `//select[@formcontrolname='rowLimitField']/option[value= '50']` seems to be within ` – undetected Selenium Apr 01 '19 at 04:00
0

Sometimes .click() does not work and I am unsure why. When that happens try .send_keys(Keys.ENTER)

I used Firefox and left my geckodriver executable path commented out in the code below. I am also locating the element by the id and not css selector.

from selenium import webdriver

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains


my_url = 'https://sbs.naic.org/solar-external-lookup/lookup?jurisdiction=AL&searchType=Company&companyStatus=AC'

driver = webdriver.Chrome('D:/chromedriver')
# driver = webdriver.Firefox(executable_path=r'C:\\Py\\geckodriver.exe');

driver.get(my_url)

click_search = driver.find_element_by_id("submitBtn")
click_search.send_keys(Keys.ENTER)
Jortega
  • 3,616
  • 1
  • 18
  • 21