技术咨询:求指定Ruby代码对应的Selenium WebDriver Java代码
Converting Ruby Selenium Code to Java for FlyDubai Date Picker
No problem! Let's translate that Ruby Selenium line into Java and slot it into your existing FlyDubai date picker test smoothly.
First, here's the direct Java equivalent of your Ruby code:
driver.findElements(By.cssSelector("button[data-pika-year='2017'][data-pika-month='11'][data-pika-day='22']")).get(0).click();
Quick Conversion Breakdown:
- Selenium Java uses
findElementsjust like your Ruby code, which returns a list of matching web elements. - Grabbing the first element with
.get(0)works identically in both languages. - The CSS selector string stays exactly the same—it's language-agnostic, so no tweaks needed there.
Here's the full integrated test method with this date selection line added:
public void FlyDubaiDatePickerTest() throws InterruptedException { // Go to website driver.get("https://www.flydubai.com/en/"); // *****Departure day selection started. // Click Departure Date driver.findElement(By.id("departureDate")).click(); // Select the specific target date (converted from Ruby code) driver.findElements(By.cssSelector("button[data-pika-year='2017'][data-pika-month='11'][data-pika-day='22']")).get(0).click(); // Add any follow-up test steps you need here }
One small reminder: Double-check that you've imported all required Selenium classes (like org.openqa.selenium.By and org.openqa.selenium.WebDriver) at the top of your Java file to avoid compilation issues.
内容的提问来源于stack exchange,提问作者Fatna
相关产品推荐
相关产品推荐

