python - Selenium 脚本向元素返回 null

标签 python selenium web-scraping

我正在运行这个脚本,它在找到 id 后获取一些内容,该 id 将由 AJAX 调用填充,它应该转到第二个 URL。转到第二个 URL 后,没有找到任何具有相同 id 的内容。

代码片段在这里:

from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import json
from selenium.webdriver.support import expected_conditions as EC

path_to_chromedriver = 'D:\Mangilal\Downloads\chromedriver_win32\chromedriver.exe'  #Change path 
as needed.
browser = webdriver.Chrome(executable_path=path_to_chromedriver)
#Sample list of URLs.

lists_of_url = ['http://facebook.com', 'http://twitter.com', 
                'http://google.com', 'http://youtube.com',
                'http://linkedin.com', 'http://wordpress.org', 
                'http://instagram.com', 'http://pinterest.com',
                'http://wikipedia.org', 'http://wordpress.com',
                'http://slideshare.net', 'http://e-recht24.de', 
                'http://washingtonpost.com', 'http://etsy.com',
                'http://eventbrite.com', 'http://archive.org', 
                'http://cpanel.net', 'http://miibeian.gov.cn',
                'http://sourceforge.net', 'http://telegraph.co.uk', 
                'http://ameblo.jp', 'http://amazon.co.uk',
                'http://ebay.com', 'http://fc2.com',
                'http://free.fr', 'http://bing.com']


for i in range(10):
    url = 'https://www.shareaholic.com/sharecounter?url=' + lists_of_url[i]
    browser.get(url)
    element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="container"]/div[1]/div[2]/div[3]/div/div/h1/span')))
    str = ''
    #Finding element here.
    count = browser.find_element_by_xpath('//*[@id="container"]/div[1]/div[2]/div[3]/div/div/h1/span')
    str = count.get_attribute('innerHTML')
    print(str)
其输出如图所示,它获取该 null 值之后的第一个 URL 的内容:

Image showing the output

最佳答案

首先,str是Python的内置函数。不要将其用作变量。
其次,应该对查询字符串进行编码。
第三,您应该等到元素可见。请参阅下面我的代码。

from urllib.parse import urlencode
...
for i in range(10):
    url = 'https://www.shareaholic.com/sharecounter?' + urlencode({"url":lists_of_url[i]})
    driver.get(url)
    element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="container"]/div[1]/div[2]/div[3]/div/div/h1/span')))
    txt = ''
    #Finding element here.
    count = driver.find_element_by_xpath('//*[@id="container"]/div[1]/div[2]/div[3]/div/div/h1/span')
    txt = count.get_attribute('innerHTML')
    print(txt)

关于python - Selenium 脚本向元素返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47822369/

相关文章:

python - Scrapy 是否可以从原始 HTML 数据中获取纯文本?

python - 如何在数组中找到N个连续数字相等?

python optparse 如何设置列表的参数?

python - Selenium 嵌套选择器无法按预期工作

selenium - Protractor E2E 测试 : email verification after creating an account

javascript - 使用nodejs抓取完全渲染的网页

python - 当给定数据框中的数组元素时,如何在同一行上找到另一个元素

python - Nose 忽略使用自定义装饰器的测试

java - Chrome 选项 - Selenium 3.10 - NoSuchMethodError : com. google.common.collect.ImmutableList.toImmutableList()

python - 在 url 错误中搜索单词