使用Python Selenium进行Web抓取:无法找到元素并滚动,出现“无法聚焦元素”错误

时间:2019-02-07 00:08:30

标签: python html selenium selenium-webdriver web-scraping

我正在检索VividSeats网站以获取机票信息。

url ='https://www.vividseats.com/nhl-hockey/los-angeles-kings-tickets/kings-vs-canucks-2-14-2751065.html'

我正在使用Selenium和Python。 我可以使用Chrome Webdriver转到页面,然后单击出现的弹出窗口。我尝试在

下的左侧刮取票证信息
<ul class='ticket-rows'> 

通过使用driver.find_elements_by_class_name.

但是,它只是刮擦列表的前半部分。向下滚动框架后,将出现更多票证。显然,只是刮刮并没有得到那张票的下半部分。

我尝试获取票证类别,然后使用

import Keys
send_keys(Keys.END) 
在元素上的

滚动到框架的末尾,然后刮取票证。但是我不断得到

WebDriverException: Message: unknown error: cannot focus element

错误。

我正在寻找框架,但是没有iframe可供我切换。这是页面内的嵌入式框架,我无法切换到该框架以滚动到页面底部。

我需要做些什么才能滚动到此框的底部,以便我可以刮取门票?我不是在问如何抓取,而是想知道如何将其滚动到最后

谢谢

编辑:我已按照@ DebanjanB的要求尝试过包含的代码

从selenium.webdriver.common.keys导入密钥

1)

scroll = driver.find_element_by_class_name('value-score')
scroll.click()
scroll.send_keys(Keys.END)

2)

scroll = driver.find_element_by_class_name('row-container')
scroll.send_keys(Keys.END)

3)

scroll = driver.find_element_by_class_name('row')
scroll.send_keys(Keys.END)

他们最终都返回错误

WebDriverException: Message: unknown error: cannot focus element

1 个答案:

答案 0 :(得分:0)

如果要剪贴整个列表,则需要向下滚动,否则元素将不可见。

因此,请尝试下面的代码,该代码将首先向下滚动,直到没有剩余元素,然后检索信息并为您打印。

// Some driver initialization code
driver.get("https://www.vividseats.com/nhl-hockey/los-angeles-kings-tickets/kings-vs-canucks-2-14-2751065.html");
driver.findElement(By.xpath("//button[text()='SKIP']")).click();
String xPath = "//ul[@class='ticket-rows']/article";
// Getting initial visible rows
List<WebElement> rows = driver.findElements(By.xpath(xPath));
while(true) {
    // Scrolling down to the last row so that remaining rows get visible
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", rows.get(rows.size()-1));
    // Getting latest loaded visible rows count
    List<WebElement> tempRows = driver.findElements(By.xpath(xPath));
    //  System.out.println(rows.size()+" <<<===>>> "+tempRows.size());
    // If the latest rows count is greater than the previous rows count then we need to do the scroll down again otherwise break the loop
    if(tempRows.size() > rows.size()) {
        rows = tempRows;
    } else {
        break;
    }
}
// Printing the name and the price, you can change it to your convenience
List<WebElement> data = driver.findElements(By.xpath(xPath+"//strong"));
for(WebElement element : data) {
    System.out.println(element.getText().trim());
}

下面是Python中的端到端代码:

from selenium import webdriver
driver = webdriver.Chrome('C:\\NotBackedUp\\chromedriver.exe')
driver.get('https://www.vividseats.com/nhl-hockey/los-angeles-kings-tickets/kings-vs-canucks-2-14-2751065.html')

popUp = driver.find_element_by_xpath("//button[text()='SKIP']");
popUp.click()

xPath = "//ul[@class='ticket-rows']/article";
rows = driver.find_elements_by_xpath(xPath)
while True:
    driver.execute_script("arguments[0].scrollIntoView(true);", rows[-1])
    tmpRows = driver.find_elements_by_xpath(xPath)
    if len(tmpRows) > len(rows) :
        rows = tmpRows
    else:
        break

for element in driver.find_elements_by_xpath(xPath+"//strong"):
    print(element.text)

print("Done...")

希望对您有帮助...

相关问题