0

I have to scrape data from a page which described bottom, my goal is to get phone number which described in the blue square on the left bottom , but the phone number is not quite visible, it will be shown fully only after clicking at.

I tested by Selenium, but no result, it returns NoSuchElementException error

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

s = Service('home/Downloads/chromedriver')
driver = webdriver.Chrome(service=s)
driver.get("https://bina.az/items/3141057")
time.sleep(5)
button = driver.find_element(By.CLASS_NAME,"show-phones js-show-phones active")

1 Answer 1

0

There are several issues here:

  1. The element you trying to click is out of the visible screen. SO, you first need to scroll the screen in order to access it.
  2. show-phones js-show-phones active are 3 class names. If you want to use all these 3 values you should use CSS_SELECTOR instead of CLASS_NAME but since show-phones is enough to make unique locator you can use it alone with CLASS_NAME.
  3. Instead of hardcoded sleeps time.sleep(5) WebDriverWait expected_conditions explicit waits should be used.

The following code works:

import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)

wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)

url = "https://bina.az/items/3141057"
driver.get(url)



element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "show-phones")))
driver.execute_script("arguments[0].scrollIntoView();", element)
time.sleep(0.5)
element.click()

The result is

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.