不能通过文本点击div元素?

时间:2021-09-15 21:04:12

I am trying to automate some downloads for my employer. I am using selenium with python and chromedriver.

我正在尝试为我的雇主自动化一些下载。我使用的是python和chromedriver的selenium。

I'm stuck at clicking this div element which is a part of a javascript drop down.

我卡住了,点击这个div元素,这是javascript的一部分。

<div class="item " idx="2" style="" title="">N53AC : BABY MILK UP TO 6 MONTHS</div>

I've been able to click this button using vbscript by looking for the innerText attribute in internet explorer.

通过在internet explorer中查找innerText属性,我可以使用vbscript点击这个按钮。

I've already tried this

我已经试过这个

elemf = driver.find_elements_by_tag_name("div")

for element in elemf:
    #print(element.get_attribute("innerhtml"))
    if element.get_attribute("innerhtml")=="N53AC : BABY MILK UP TO 6 MONTHS" or element.get_attribute("innertext")=="N53AC : BABY MILK UP TO 6 MONTHS" or element.text=="N53AC : BABY MILK UP TO 6 MONTHS":
        element.click()

Any leads? I am a complete newbie in html.

领导吗?我是一个完整的html新手。

1 个解决方案

#1


0  

Here is the Answer to your Question:

下面是你的问题的答案:

The main issue in your code is you are trying to find_elements_by_tag_name("div"); which will be numerous on the current HTML DOM. Instead you need to narrow down your search to something like:

您的代码中的主要问题是您正在尝试find_elements_by_tag_name(“div”);这将在当前的HTML DOM中大量出现。相反,你需要缩小搜索范围,比如:

elemf = driver.find_elements_by_xpath("//div[@class='item ']");

Now we can get into a loop and call click() method as follows:

现在我们可以进入一个循环并调用click()方法,如下所示:

for element in elemf:
    my_attribute = element.get_attribute("innerHTML")
    if my_attribute == "N53AC : BABY MILK UP TO 6 MONTHS"
        element.click()
        break

Let me know if this Answers your Question.

如果这回答了你的问题,请告诉我。

#1


0  

Here is the Answer to your Question:

下面是你的问题的答案:

The main issue in your code is you are trying to find_elements_by_tag_name("div"); which will be numerous on the current HTML DOM. Instead you need to narrow down your search to something like:

您的代码中的主要问题是您正在尝试find_elements_by_tag_name(“div”);这将在当前的HTML DOM中大量出现。相反,你需要缩小搜索范围,比如:

elemf = driver.find_elements_by_xpath("//div[@class='item ']");

Now we can get into a loop and call click() method as follows:

现在我们可以进入一个循环并调用click()方法,如下所示:

for element in elemf:
    my_attribute = element.get_attribute("innerHTML")
    if my_attribute == "N53AC : BABY MILK UP TO 6 MONTHS"
        element.click()
        break

Let me know if this Answers your Question.

如果这回答了你的问题,请告诉我。