有没有一种方法可以使用Selenium Webdriver(Python)捕获样式数据?

时间:2019-11-25 22:02:28

标签: html python-3.x selenium selenium-webdriver selenium-chromedriver

我目前正在寻找一家公司用来样式化我的特定字体(例如,对于http://stylifyme.com/?stylify=uber.com,我想删除“ UberMove,'Open Sans','Helvetica Neue',Helvetica,sans-serif,正常,52像素,56像素,#000000“)。但是,在最终拉出文本时遇到了问题-文本显示在html中,但是当我尝试拉出文本时却没有出现。我尝试过提取内部HTML和文本,请参见下面的示例代码和文本。

page=webdriver.Chrome('/Downloads/chromedriver.exe')
page.get('http://stylifyme.com/')
website_finder=page.find_element_by_id('input-stylify')
website_finder.send_keys('www.bcg.com')
website_finder.submit()

#try 1:
print(page.find_element_by_id("result-header-1-dt").text)
#output 1: "Header 1: Font, Style, Size, Leading, Colour"

#try 2
print(page.find_element_by_xpath('/html/body/div[1]/table/tbody/tr[1]/th/strong').get_attribute("innerHTML"))
#output 2: "Header 1:"


HTML代码:

<th id="result-header-1-dt" class="first" scope="row"><strong style="opacity: 1;">
UberMove, 'Open Sans', 'Helvetica Neue', Helvetica, sans-serif, normal, 52px, 56px, #000000
</strong> <span style="opacity: 1;">Font, Style, Size, Leading, Colour</span></th>

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

如pguardiario所述,解决方案是等待元素被加载。在大多数情况下,使用[Authorize] [ApiController] [ApiVersionNeutral] [ApiConventionType(typeof(DefaultApiConventions))] [Route("v{version:apiVersion}/{providerId}/[controller]")] public class GroupsController : ControllerBase { //...omitted code... public GroupsController(IGroupsService groupsService, LinkGenerator linkGenerator) { this.groupsService = groupsService; this.linkGenerator = linkGenerator; } // GET: api/0000/groups/1000 // Get a group [HttpGet("{groupId}", Name="GetGroup")] [ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Get))] public async Task<ActionResult<Group>> GetGroup(int providerId, int groupId) { Group group = await this.groupsService.GetGroup(providerId, providerId, groupId); if (group == null) return NotFound(); else group.Url = linkGenerator.GetUriByAction(HttpContext, action: "GetGroup", controller: "Groups", values: new { providerId, groupId }); return Ok(group); } // GET: api/0000/groups // Get all groups [HttpGet] [ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Get))] public async Task<ActionResult<IEnumerable<Group>>> GetGroups(int providerId) { IEnumerable<Group> groups = await groupsService.GetGroups(providerId, providerId); if (groups == null) return NotFound(); else { foreach (Group g in groups) { g.Url = linkGenerator.GetUriByAction(HttpContext, action: "GetGroup", controller: "Groups", values: new { providerId = g.ProviderID, groupId = g.GroupID }); } } return Ok(groups); } //...omitted code... 可以很好地工作,但是经常使用time.sleep(5)可以更好地工作。 WebDriverWait会休眠一段时间,这可能导致运行脚本时出现不必要的暂停,或者如果页面的加载时间很长,则会导致失败。 time.sleep通过在找到元素后完成来帮助保持脚本运行。如果从未找到该元素,则将引发Exception。

WebDriverWait