Py学习  »  Python

如何使用selenium python在oddsportal中使用日期查找链接

Vigneswaran • 3 年前 • 1604 次点击  

我需要一个解决这个问题的办法 oddsportal 我需要一个特定的日期来匹配我的名字

Example: 

user input = 23 Jan 2022

output:

AC Milan - Juventus

Empoli - AS Roma

Napoli - Salernitana

Spezia - Sampdoria

Torino - Sassuolo

Cagliari - Fiorentina

不接受下一个日期匹配名称只需要用户输入日期匹配名称 像这样我需要链接

dates = driver.find_elements_by_xpath("//span[contains(@class,'datet')]")
for i in dates:
    # print(i.text)
    if i.text == date:
        print(i.text)
        lin = driver.find_elements(By.XPATH,'//*[@id="tournamentTable"]/tbody/tr/td[2]/a')
        for link in lin:
            if i.text == date:
                print(link.text)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132844
 
1604 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Muhammad Farooq
Reply   •   1 楼
Muhammad Farooq    3 年前

以下是解决方案 JAVA


        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(100));
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(50));
        driver.get("https://www.oddsportal.com/soccer/italy/serie-a/results/");
        driver.manage().window().maximize();

        List<WebElement> dates = driver.findElements(By.cssSelector("tr[class='center nob-border']"));
        List<WebElement> siblings;

        for (WebElement element: dates) {
            if ((element.findElement(By.cssSelector("th > span"))).getText().equalsIgnoreCase(userDate)){
                siblings=element.findElements(By.xpath("following-sibling::tr"));
                for (int i=1; i< siblings.size() ; i++ ){
                    try {
                        WebElement item = siblings.get(i).findElement(By.cssSelector("td[class='name table-participant'] > a"));
                        if (!(item.isDisplayed())) {
                            break;
                        } else
                            System.out.println(" " + item.getText());
                    }
                    catch (Exception e){
                        e.printStackTrace();
                        break;
                    }
                }
                break;
            }
        }

解决方案: 首先找到日期,然后迭代到下一个日期。