Webdriver: Locate typefield via placeholder text then text via sendkey("test text") into the typefield

huangapple go评论112阅读模式
英文:

Webdriver: Locate typefield via placeholder text then text via sendkey("test text") into the typefield

问题

我一直在尝试发送文本到一个文本字段,该字段的更改元素为id=PolarisTextField83,每次登录到页面时(PolarisTextField##的值都会像id=PolarisTextField45一样更改),因为我发现id元素是动态的,而HTML中唯一静态且唯一的部分是示例文本占位符,其为placeholder="e.g. Shirts"

因此,我想知道是否有一种方法可以定位placeholder="e.g. Shirts",然后将文本发送到相应的类型字段(PolarisTextField##)?

我尝试过使用

driver.findElementsByTagName("e.g. Shirts").sendKeys("test text");

但我明白在findElementsByTagName后面不能使用.sendKeys()

我对Java和Selenium都很新,会感激任何帮助和/或指导!

英文:

I have been trying to send a text to a text field that has the changing element id=PolarisTextField83 each time a log into a page (PolarisTextField## keeps changing its value like id=PolarisTextField45) as I have found that the id element is dynamic and the only static and unique part of the HTML is the placeholder example text which is placeholder="e.g. Shirts".

Therefore, I wonder if there is a way of locating placeholder="e.g. Shirts" then sending text to its respective type field (PolarisTextField##)?

I have tried to use

driver.findElementsByTagName("e.g. Shirts").sendKeys("test text");

but came to the understanding that I cannot follow .sendKeys() after findElementsByTagName.

I am new to both java and selenium and would appreciate any help and/or guidance!!

答案1

得分: 1

你可以使用XPath通过占位符值来查找元素:

driver.findElement(By.xpath("//*[@placeholder='e.g. Shirts']")).sendKeys("测试文本");
英文:

You can find the element by placeholder value using XPath

driver.findElement(By.xpath("//*[@placeholder='e.g. Shirts']")).sendKeys("test text");

答案2

得分: 0

尝试以下XPath来处理动态网页元素。

WebDriverWait wait = new WebDriverWait(driver, 30);
driver.findElement(By.xpath("//*[starts-with(@id,'PolarisTextField')]").sendKeys("Test");
英文:

Try below xpath to deal with dynamic web element.

WebDriverWait wait = new WebDriverWait(driver, 30);
driver.findElement(By.xpath("//*[starts-with(@id,'PolarisTextField')]")).sendKeys("Test");

答案3

得分: 0

  • 使用 css_selectoridplaceholder 属性:
driver.findElement(By.cssSelector("[id^='PolarisTextField'][placeholder$='Shirts']")).sendKeys("test text");
  • 使用 xpathidplaceholder 属性:
driver.findElement(By.xpath("//*[starts-with(@id, 'Shirts') and contains(@placeholder, 'Shirts')]")).sendKeys("test text");

理想情况下,要在元素中发送 字符序列,您需要使用 WebDriverWait 来等待 elementToBeClickable(),然后可以使用以下任一 定位策略

  • cssSelector
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("[id^='PolarisTextField'][placeholder$='Shirts']"))).sendKeys("test text");
  • xpath
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[starts-with(@id, 'Shirts') and contains(@placeholder, 'Shirts')]"))).sendKeys("test text");
英文:

To send a character sequence within the element you can use either of the following Locator Strategies:

  • Using css_selector, id and placeholder attributes:

    driver.findElement(By.cssSelector("[id^='PolarisTextField'][placeholder$='Shirts']")).sendKeys("test text");
    
  • Using xpath, id and placeholder attributes:

    driver.findElement(By.xpath("//*[starts-with(@id, 'Shirts') and contains(@placeholder, 'Shirts')]")).sendKeys("test text");
    

Ideally, to send a character sequence within the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("[id^='PolarisTextField'][placeholder$='Shirts']"))).sendKeys("test text");
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[starts-with(@id, 'Shirts') and contains(@placeholder, 'Shirts')]"))).sendKeys("test text");
    

huangapple
  • 本文由 发表于 2020年7月29日 19:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63152178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定