Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Selenium中创建具有大量id值和类名属性的自定义定位器_Selenium_Selenium Webdriver_Xpath_Css Selectors_Webdriverwait_Python_Java - Fatal编程技术网

如何在Selenium中创建具有大量id值和类名属性的自定义定位器

如何在Selenium中创建具有大量id值和类名属性的自定义定位器,selenium,selenium-webdriver,xpath,css-selectors,webdriverwait,python,java,Selenium,Selenium Webdriver,Xpath,Css Selectors,Webdriverwait,Python,Java,有人能帮我用下面的html片段生成Xpath吗。 我正在努力为它创建xpath,因为id、类都很大…请帮助如何为此生成xpath <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-

有人能帮我用下面的html片段生成Xpath吗。 我正在努力为它创建xpath,因为id、类都很大…请帮助如何为此生成xpath

<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">


如果您的类是唯一的,您可以使用它:

//input[@class="js-text-full text-full form-text required"]
如果您认为类名太长,可以选择contains并提及部分匹配字符串:

//input[contains(@class,"form-text required")]
如果您的类不是唯一的,您可以尝试将其与其他属性(如type)组合使用

//input[contains(@class,"form-text required") and @type="text"]
或者您可以将其与类名和部分匹配id结合使用:

//input[@class="js-text-full text-full form-text required" and contains(@id,"edit-page-content")]

使用此Xpath//input[@class=“js text full text full form text required”]

如果它不起作用,请在这里共享您的Html元素

在构建逻辑时,id或类名的较大值不应成为障碍。它是一个
元素,可能向前移动时,您将与该元素交互,并且由于该元素是一个动态元素,因此您需要对
元素进行归纳,使其可伸缩()
,并且您可以使用以下任一项:

  • 使用和
    css选择器

    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()
    
  • 使用和
    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']")));
    

你说的“为此生成Xpath”是什么意思?对不起,我的错。我的意思是说如何创建自定义Xpath。你是说你有一大块html,问题中的
标记是该html的一部分,你想在该html中找到该
的Xpath吗?如果是这种情况,您可能需要粘贴该html的代表性示例。很难或不可能生成“通用”xpath。对于类,它不起作用,因为类名有空格。不管类名是否有空格,只要通过在浏览器上检查并使用它来复制元素即可