2

Can someone help me with how to generate Xpath with the below html snippet. I am struggling to make xpath for it , as the id ,class to are big ...please help how to generate Xpath for this.

<input class="js-text-full text-full form-text required" data-drupal-selector="edit-page-content-0-subform-page-layout-content-0-subform-slot-1-0-subform-slot-content-form-inline-entity-form-module-type-content-0-subform-module-a-content-0-subform-large-composite-image-carousel-0-subform-large-composite-images-0-subform-header-text-0-subform-header-text-header-text-lines-0-subform-header-text-line-text-items-0-subform-text-items-items-0-subform-text-styled-text-0-value" type="text" id="edit-page-content-0-subform-page-layout-content-0-subform-slot-1-0-subform-slot-content-form-inline-entity-form-module-type-content-0-subform-module-a-content-0-subform-large-composite-image-carousel-0-subform-large-composite-images-0-subform-header-text-0-subform-header-text-header-text-lines-0-subform-header-text-line-text-items-0-subform-text-items-items-0-subform-text-styled-text-0-value--5OtB_Vbe-qw" name="page_content[0][subform][page_layout_content][0][subform][slot_1][0][subform][slot_content][form][inline_entity_form][module_type_content][0][subform][module_a_content][0][subform][large_composite_image_carousel][0][subform][large_composite_images][0][subform][header_text][0][subform][header_text__header_text_lines][0][subform][header_text_line__text_items][0][subform][text_items__items][0][subform][text_styled_text][0][value]" value="" size="60" maxlength="255" placeholder="" required="required" aria-required="true">

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • What do you mean by "generate Xpath for this"? – Jack Fleeting Jul 26 '20 at 12:11
  • Sorry my bad ..I meant by saying how to create customise Xpath. – Priyanka Dadhwal Jul 26 '20 at 12:19
  • Are you saying that you have a chunk of html, that the `` tag in the question is part of that html and you want to find the xpath of this `` in that html? If that's the case, you may need to paste a representative sample of that html. It's difficult, or impossible, to generate a "universal" xpath. – Jack Fleeting Jul 26 '20 at 12:23

3 Answers3

1

if your class is unique, you can just use it:

//input[@class="js-text-full text-full form-text required"]

if you think class name is too long you can go for contains and mention partial matching string:

//input[contains(@class,"form-text required")]

if your class is not unique you can try using it with combination of other attributes like type

//input[contains(@class,"form-text required") and @type="text"]

Or you can use it with the combination of class name and partial matching id:

//input[@class="js-text-full text-full form-text required" and contains(@id,"edit-page-content")]
Ayaz
  • 249
  • 1
  • 2
  • 11
0

Use this Xpath //input[@class="js-text-full text-full form-text required"]

if its not working pls share your Html elements here

Justin Lambert
  • 940
  • 1
  • 7
  • 13
0

Larger value of id or class name shouldn't be a barrier when you construct logical Locator Strategies. It's an <input> element and possibly moving forward you'd interact with the element and as the element is a dynamic element, so you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using and cssSelector:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.js-text-full.text-full.form-text.required[data-drupal-selector^='edit-page-content'][maxlength='255'][placeholder]"))).click()
    
  • Using and xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='js-text-full text-full form-text required' and starts-with(@data-drupal-selector, 'edit-page-content')][@placeholder and @maxlength='255']")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352