Home > Enterprise >  full xpath does not math the correct field in python selenium
full xpath does not math the correct field in python selenium

Time:01-06

I have a following problem. On the picture bellow I would like to fill some text into the second (red) field.

enter image description here

My code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains

def set_scraper():
    """Function kills running applications and set up the ChromeDriver."""
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", options=options)
    return driver


def main() -> None:
    """Main function that is call when the script is run."""
    driver = set_scraper()
    driver.get("https://nahlizenidokn.cuzk.cz/VyberBudovu/Stavba/InformaceO")

    pokus = driver.find_element(By.XPATH, '/html/body/form/div[5]/div/div/div/div[3]/div/fieldset/div[2]/div[2]/input[1]')
    
    driver.implicitly_wait(10)
    ActionChains(driver).move_to_element(pokus).send_keys("2727").perform()
    

The problem is that it sends "2727" into the first field, not into the red one. Although /html/body/form/div[5]/div/div/div/div[3]/div/fieldset/div[2]/div[2]/input[1] is the full xpath of the second field. Do you know why, please?

CodePudding user response:

You can use XPath to locate the parent element based on unique text "Obec" in the child element and then locate the proper input element.
Here I'm using fixed attribute values that not seem to change.
The following code works:

from selenium import webdriver
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(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)

url = "https://nahlizenidokn.cuzk.cz/VyberBudovu/Stavba/InformaceO"
driver.get(url)

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='fieldsetWrapper'][contains(.,'Obec')]//input[@type='text']"))).send_keys("2727")

The result is:

enter image description here

CodePudding user response:

Try with below

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


element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@title='Zadejte název obce']")))
element.send_keys("2727")

CodePudding user response:

You can enter the text in the second text field using the below XPATH:

driver.find_element(By.XPATH, ".//input[@name='ctl00$bodyPlaceHolder$vyberObec$txtObec']").send_keys("2727")
# clicking on the button
driver.find_element(By.XPATH, ".//input[@title='Vyhledat obec']").click()
  • Related