Issue
I am working on a Python scraping code to scrape the website id. It has 29 rows on the webpage with a unique id for each row. Here is my code
op = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=op, executable_path="/usr/local/bin/chromedriver")
driver.get(web)
driver.find_element_by_xpath('//*[@id="caseCriteria_SearchCriteria"]').send_keys(keys)
input("Press Enter to continue...")
content = driver.find_elements_by_class_name('k-detail-cell')
for c in content:
grid = c.find_element_by_css_selector('.party-card')
g = grid.get_attribute('id')
print(g)
driver.close()
I was able to get the id from the first row but then it gave an error NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".party-card"} (Session info: chrome=92.0.4515.131)
I am wondering am I doing it correctly? I attached a screenshot of the page source as well. every row are identical for the <div class='data-party-id' ....>
That will be great if I have get some advice!
Thanks!
Solution
OK, I will give you Selenium approach solution.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
op = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=op, executable_path="/usr/local/bin/chromedriver")
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get(web)
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="caseCriteria_SearchCriteria"]')))
#here you should pass the CAPTCHA....
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="caseCriteria_SearchCriteria"]'))).send_keys(keys)
driver.find_element_by_xpath("//p//input[@id='btnSSSubmit']").click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".k-detail-cell .party-card")))
time.sleep(2)
grid = driver.find_elements_by_css_selector(".k-detail-cell .party-card")
for g in grid:
actions.move_to_element(g).perform()
time.sleep(0.5)
print(g.get_attribute('id'))
Answered By - Prophet
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.