Home > Software design >  How in Selenium scraping by xpath?
How in Selenium scraping by xpath?

Time:05-30

I would like to click the button on page: enter image description here I write example code like this below:

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

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)
url='https://igs.org/network/'
driver.get(url)
time.sleep(4)
myxpath = '/html/body/main/div/div/div[2]/div/div[2]/div[1]/div[2]/button[2]'
el = driver.find_element_by_xpath(myxpath).click()

and I have error like this

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/main/div/div/div2/div/div2/div1/div2/button2"}

What I'm doing wrong? Does anyone have any ideas for navigating these elements?

CodePudding user response:

The element you are trying to click is inside an iframe. You need to swith to iframe first in order to access the element.

Use WebDriverWait() and wait for frame available and switch it and following css selector.

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

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)
url='https://igs.org/network/'
driver.get(url
wait=WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src='/imaps/map.html']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.toggler'))).click()

CodePudding user response:

The xpath for that button is this one //*[@id="side-controls"]/button[2]

  • Related