python - 如何使用 Selenium + Python 向下滚动对话框
问题描述
我有一个来自医疗网络应用程序的对话框,它在第一个屏幕上只显示 30 名患者,要查看更多患者,我向下滚动对话框,当我接近第 30 名患者时,它会显示更多 30 名患者。
当我在新窗口中打开他们的链接配置文件并收集信息并保存在 xmls 文件中时,我试图在该对话框中显示 2000 多个 paciente。
如何使用 Python + Selenium 向下滚动?
这是对话框屏幕:
这个 html 是第 6 个第一个患者和第 30 个的对话框,我无法发布所有 30 个(字符限制):
<md-dialog aria-label="Pacientes" ng-form="mainForm" flex="70" flex-xl="40" class="ng-pristine ng-valid _md flex-xl-40 flex-70 md-transition-in" role="dialog" tabindex="-1" aria-describedby="dialogContent_573" style="">
<md-toolbar md-theme="white" class="_md md-white-theme _md-toolbar-transitions">
<div class="md-toolbar-tools layout-wrap layout-row" layout="row" layout-wrap="">
<h2 flex="100" flex-gt-xs="50" class="flex-gt-xs-50 flex-100">Pacientes</h2>
<md-input-container flex="100" flex-gt-xs="50" md-no-float="" style="padding: 0;" class="md-input-has-placeholder md-white-theme flex-gt-xs-50 flex-100">
<input type="text" placeholder="Pesquisar" ng-model="pacienteCtrl.search" ng-change="pacienteCtrl.onChangeSearch()" class="ng-pristine ng-untouched ng-valid md-input ng-empty" id="input_572" aria-invalid="false" style=""><div class="md-errors-spacer"></div>
</md-input-container>
</div>
</md-toolbar>
<md-dialog-content class="md-dialog-content" when-scrolled="pacienteCtrl.nextPage()" id="dialogContent_573">
<div layout="column" class="layout-column">
<md-divider></md-divider>
<md-list class="sd-list-pacientes" role="list">
<!----><md-list-item class="md-2-line md-no-proxy _md" ng-repeat="paciente in pacienteCtrl.pacientes" paciente-link="" paciente="paciente" role="listitem" style="">
<img ng-src="assets/img/user-default.png" class="md-avatar" src="assets/img/user-default.png">
<div layout="column" class="md-list-item-text layout-column">
<a ng-click="go()" class="sd-link" paciente="paciente" aria-label="Ir para a ficha desse paciente" md-labeled-by-tooltip="md-tooltip-574" ng-href="https://app.simplesdental.com/simples/pacientes/11489833/sobre" href="https://app.simplesdental.com/simples/pacientes/11489833/sobre">
Abraão Laurentino dos Santos
</a>
<div layout="column" layout-gt-sm="row" class="sd-list-pacientes-info layout-gt-sm-row layout-column">
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.celular">
<md-icon md-svg-icon="phone" role="img" aria-label="phone"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M31 2H15c-2.76 0-5 2.24-5 5v34c0 2.76 2.24 5 5 5h16c2.76 0 5-2.24 5-5V7c0-2.76-2.24-5-5-5zm-8 42c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm9-8H14V8h18v28z"></path></svg></md-icon>
<span class="grey">(xx) xxxxx-xxxx</span>
</span><!---->
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.cpf">
<md-icon md-svg-icon="person" role="img" aria-label="person"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M24 24c4.42 0 8-3.59 8-8 0-4.42-3.58-8-8-8s-8 3.58-8 8c0 4.41 3.58 8 8 8zm0 4c-5.33 0-16 2.67-16 8v4h32v-4c0-5.33-10.67-8-16-8z"></path></svg></md-icon>
<span class="grey">xxx.xxx.xxx-xx</span>
</span><!---->
</div>
</div>
<md-divider></md-divider>
<div class="md-secondary-container"></div></md-list-item><!----><md-list-item class="md-2-line md-no-proxy _md" ng-repeat="paciente in pacienteCtrl.pacientes" paciente-link="" paciente="paciente" role="listitem">
<img ng-src="assets/img/user-default.png" class="md-avatar" src="assets/img/user-default.png">
<div layout="column" class="md-list-item-text layout-column">
<a ng-click="go()" class="sd-link" paciente="paciente" aria-label="Ir para a ficha desse paciente" md-labeled-by-tooltip="md-tooltip-575" ng-href="https://app.simplesdental.com/simples/pacientes/11827315/sobre" href="https://app.simplesdental.com/simples/pacientes/11827315/sobre">
Acyr Xavier Moreira
</a>
<div layout="column" layout-gt-sm="row" class="sd-list-pacientes-info layout-gt-sm-row layout-column">
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.celular">
<md-icon md-svg-icon="phone" role="img" aria-label="phone"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M31 2H15c-2.76 0-5 2.24-5 5v34c0 2.76 2.24 5 5 5h16c2.76 0 5-2.24 5-5V7c0-2.76-2.24-5-5-5zm-8 42c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm9-8H14V8h18v28z"></path></svg></md-icon>
<span class="grey">(xx) xxxxx-xxxx</span>
</span><!---->
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.cpf">
<md-icon md-svg-icon="person" role="img" aria-label="person"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M24 24c4.42 0 8-3.59 8-8 0-4.42-3.58-8-8-8s-8 3.58-8 8c0 4.41 3.58 8 8 8zm0 4c-5.33 0-16 2.67-16 8v4h32v-4c0-5.33-10.67-8-16-8z"></path></svg></md-icon>
<span class="grey">xxx.xxx.xxx-xx</span>
</span><!---->
</div>
</div>
<md-divider></md-divider>
<div class="md-secondary-container"></div></md-list-item><!----><md-list-item class="md-2-line md-no-proxy _md" ng-repeat="paciente in pacienteCtrl.pacientes" paciente-link="" paciente="paciente" role="listitem">
<img ng-src="assets/img/user-default.png" class="md-avatar" src="assets/img/user-default.png">
<div layout="column" class="md-list-item-text layout-column">
<a ng-click="go()" class="sd-link" paciente="paciente" aria-label="Ir para a ficha desse paciente" md-labeled-by-tooltip="md-tooltip-576" ng-href="https://app.simplesdental.com/simples/pacientes/17258954/sobre" href="https://app.simplesdental.com/simples/pacientes/17258954/sobre">
Adilton Junior
</a>
<div layout="column" layout-gt-sm="row" class="sd-list-pacientes-info layout-gt-sm-row layout-column">
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.celular">
<md-icon md-svg-icon="phone" role="img" aria-label="phone"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M31 2H15c-2.76 0-5 2.24-5 5v34c0 2.76 2.24 5 5 5h16c2.76 0 5-2.24 5-5V7c0-2.76-2.24-5-5-5zm-8 42c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm9-8H14V8h18v28z"></path></svg></md-icon>
<span class="grey">(xx) xxxxx-xxxx</span>
</span><!---->
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.cpf">
<md-icon md-svg-icon="person" role="img" aria-label="person"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M24 24c4.42 0 8-3.59 8-8 0-4.42-3.58-8-8-8s-8 3.58-8 8c0 4.41 3.58 8 8 8zm0 4c-5.33 0-16 2.67-16 8v4h32v-4c0-5.33-10.67-8-16-8z"></path></svg></md-icon>
<span class="grey">xxx.xxx.xxx-xx</span>
</span><!---->
</div>
</div>
<md-divider></md-divider>
<div class="md-secondary-container"></div></md-list-item><!----><md-list-item class="md-2-line md-no-proxy _md" ng-repeat="paciente in pacienteCtrl.pacientes" paciente-link="" paciente="paciente" role="listitem">
<img ng-src="assets/img/user-default.png" class="md-avatar" src="assets/img/user-default.png">
<div layout="column" class="md-list-item-text layout-column">
<a ng-click="go()" class="sd-link" paciente="paciente" aria-label="Ir para a ficha desse paciente" md-labeled-by-tooltip="md-tooltip-577" ng-href="https://app.simplesdental.com/simples/pacientes/15922853/sobre" href="https://app.simplesdental.com/simples/pacientes/15922853/sobre">
Adalberto Cortes da Silva
</a>
<div layout="column" layout-gt-sm="row" class="sd-list-pacientes-info layout-gt-sm-row layout-column">
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.celular">
<md-icon md-svg-icon="phone" role="img" aria-label="phone"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M31 2H15c-2.76 0-5 2.24-5 5v34c0 2.76 2.24 5 5 5h16c2.76 0 5-2.24 5-5V7c0-2.76-2.24-5-5-5zm-8 42c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm9-8H14V8h18v28z"></path></svg></md-icon>
<span class="grey">(xx) xxxxx-xxxx</span>
</span><!---->
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.cpf">
<md-icon md-svg-icon="person" role="img" aria-label="person"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M24 24c4.42 0 8-3.59 8-8 0-4.42-3.58-8-8-8s-8 3.58-8 8c0 4.41 3.58 8 8 8zm0 4c-5.33 0-16 2.67-16 8v4h32v-4c0-5.33-10.67-8-16-8z"></path></svg></md-icon>
<span class="grey">xxx.xxx.xxx-xx</span>
</span><!---->
</div>
</div>
<md-divider></md-divider>
<div class="md-secondary-container"></div></md-list-item><!----><md-list-item class="md-2-line md-no-proxy _md" ng-repeat="paciente in pacienteCtrl.pacientes" paciente-link="" paciente="paciente" role="listitem">
<img ng-src="assets/img/user-default.png" class="md-avatar" src="assets/img/user-default.png">
<div layout="column" class="md-list-item-text layout-column">
<a ng-click="go()" class="sd-link" paciente="paciente" aria-label="Ir para a ficha desse paciente" md-labeled-by-tooltip="md-tooltip-578" ng-href="https://app.simplesdental.com/simples/pacientes/13516998/sobre" href="https://app.simplesdental.com/simples/pacientes/13516998/sobre">
Adalton Barbosa Cardoso Cunha
</a>
<div layout="column" layout-gt-sm="row" class="sd-list-pacientes-info layout-gt-sm-row layout-column">
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.celular">
<md-icon md-svg-icon="phone" role="img" aria-label="phone"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M31 2H15c-2.76 0-5 2.24-5 5v34c0 2.76 2.24 5 5 5h16c2.76 0 5-2.24 5-5V7c0-2.76-2.24-5-5-5zm-8 42c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm9-8H14V8h18v28z"></path></svg></md-icon>
<span class="grey">(xx) xxxxx-xxxx</span>
</span><!---->
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.cpf">
<md-icon md-svg-icon="person" role="img" aria-label="person"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M24 24c4.42 0 8-3.59 8-8 0-4.42-3.58-8-8-8s-8 3.58-8 8c0 4.41 3.58 8 8 8zm0 4c-5.33 0-16 2.67-16 8v4h32v-4c0-5.33-10.67-8-16-8z"></path></svg></md-icon>
<span class="grey">xxx.xxx.xxx-xx</span>
</span><!---->
</div>
</div>
<md-divider></md-divider>
<div class="md-secondary-container"></div></md-list-item><!----><md-list-item class="md-2-line md-no-proxy _md" ng-repeat="paciente in pacienteCtrl.pacientes" paciente-link="" paciente="paciente" role="listitem">
<img ng-src="assets/img/user-default.png" class="md-avatar" src="assets/img/user-default.png">
<div layout="column" class="md-list-item-text layout-column">
<a ng-click="go()" class="sd-link" paciente="paciente" aria-label="Ir para a ficha desse paciente" md-labeled-by-tooltip="md-tooltip-579" ng-href="https://app.simplesdental.com/simples/pacientes/17103848/sobre" href="https://app.simplesdental.com/simples/pacientes/17103848/sobre">
Adelina de Oliveira
</a>
<div layout="column" layout-gt-sm="row" class="sd-list-pacientes-info layout-gt-sm-row layout-column">
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.celular">
<md-icon md-svg-icon="phone" role="img" aria-label="phone"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M31 2H15c-2.76 0-5 2.24-5 5v34c0 2.76 2.24 5 5 5h16c2.76 0 5-2.24 5-5V7c0-2.76-2.24-5-5-5zm-8 42c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm9-8H14V8h18v28z"></path></svg></md-icon>
<span class="grey">(xx) xxxxx-xxxx</span>
</span><!---->
<!---->
<!---->
</div>
</div>
<md-divider></md-divider>
<div class="md-secondary-container"></div></md-list-item><!----><md-list-item class="md-2-line md-no-proxy _md" ng-repeat="paciente in pacienteCtrl.pacientes" paciente-link="" paciente="paciente" role="listitem">
<img ng-src="assets/img/user-default.png" class="md-avatar" src="assets/img/user-default.png">
<div layout="column" class="md-list-item-text layout-column">
<a ng-click="go()" class="sd-link" paciente="paciente" aria-label="Ir para a ficha desse paciente" md-labeled-by-tooltip="md-tooltip-580" ng-href="https://app.simplesdental.com/simples/pacientes/12563381/sobre" href="https://app.simplesdental.com/simples/pacientes/12563381/sobre">
Alan Albuquerque
</a>
<div layout="column" layout-gt-sm="row" class="sd-list-pacientes-info layout-gt-sm-row layout-column">
<!---->
<!----><span class="sd-list-pacientes-info-item" ng-if="paciente.celular">
<md-icon md-svg-icon="phone" role="img" aria-label="phone"><svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 48 48" fit="" preserveAspectRatio="xMidYMid meet" focusable="false"><path d="M31 2H15c-2.76 0-5 2.24-5 5v34c0 2.76 2.24 5 5 5h16c2.76 0 5-2.24 5-5V7c0-2.76-2.24-5-5-5zm-8 42c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm9-8H14V8h18v28z"></path></svg></md-icon>
<span class="grey">(xx) xxxxx-xxxx</span>
</span><!---->
<!---->
<!---->
</div>
</div>
<md-divider></md-divider>
<div class="md-secondary-container"></div></md-list-item><!---->
</md-list>
</div>
<div layout="column" ng-show="!pacienteCtrl.pacientes.length" layout-align="center center" class="layout-align-center-center layout-column ng-hide" aria-hidden="true" style="">
<p class="inset">Sem resultados para a busca</p>
</div>
</md-dialog-content>
<md-dialog-actions layout="row" layout-align="end center" class="layout-align-end-center layout-row">
<button class="md-button md-ink-ripple" type="button" ng-transclude="" ng-click="pacienteCtrl.closeDialog()"> Fechar </button>
</md-dialog-actions>
</md-dialog>
这是我试图向下滚动的代码:
element = driver.find_element(By.XPATH("//div[@class='md-dialog-container']//md-list-item[30]"))#选择内容div的最后一个child driver.execute_script( "arguments[0].scrollIntoView(true);", element) #滚动到最后一个子节点
但我收到消息错误:
element = driver.find_element(By.XPATH("//div[@class='md-dialog-container']//md-list-item[30]")) #Select last child of content div TypeError: 'str ' 对象不可调用
解决方案
此错误消息...
TypeError: 'str' object is not callable
...暗示您的程序调用了function()
实际上是 a 的 a property
。
根据 Selenium doc,这是正确的方法签名:
find_element(by='id', value=None)
Find an element given a By strategy and locator. Prefer the find_element_by_* methods when possible. Usage : element = element.find_element(By.ID, ‘foo’) Return type: WebElement
这在文档中提到
def find_element(self, by=By.ID, value=None): """ Find an element given a By strategy and locator. Prefer the find_element_by_* methods when possible. :Usage: element = element.find_element(By.ID, 'foo') :rtype: WebElement """ if self._w3c: if by == By.ID: by = By.CSS_SELECTOR value = '[id="%s"]' % value elif by == By.TAG_NAME: by = By.CSS_SELECTOR elif by == By.CLASS_NAME: by = By.CSS_SELECTOR value = ".%s" % value elif by == By.NAME: by = By.CSS_SELECTOR value = '[name="%s"]' % value return self._execute(Command.FIND_CHILD_ELEMENT, {"using": by, "value": value})['value']
因此,根据方法签名,您应该使用
last_element = driver.find_element(By.XPATH,"//div[@class='md-dialog-container']//md-list-item[30]")
driver.execute_script("arguments[0].scrollIntoView(true);", last_element)
或者
last_element = driver.find_element_by_xpath("//div[@class='md-dialog-container']//md-list-item[30]")
driver.execute_script("arguments[0].scrollIntoView(true);", last_element)
推荐阅读
- javascript - 为什么减去字符会给出错误的答案
- python - 如何从 OLS 回归结果创建变量?
- javascript - 当用户尝试使用错误的凭据登录时我应该返回什么响应以及如何在前端指示凭据错误?
- serilog - 可以按短语过滤吗?
- python - ValueError: invalid literal for int() with base 10: '' has occurred idk 解决方案
- java - 使用 AndroidTV 上的 leanback 库更改 PlaybackTransportControl 操作按钮的焦点颜色
- ruby - 没有 sudo 命令的 Jekyll 安装错误
- linux - 构建 docker:未找到 opt/conda/bin/conda
- javascript - 如何将我的值附加到谷歌电子表格 OAUTH/GAPI js
- amazon-web-services - AWS 中的密码格式的秘密