英文:
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_selector、id 和 placeholder 属性:
driver.findElement(By.cssSelector("[id^='PolarisTextField'][placeholder$='Shirts']")).sendKeys("test text");
- 使用
xpath、id 和 placeholder 属性:
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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论