使用Python和Selenium来导航Python – 无法找到链接?

huangapple go评论55阅读模式
英文:

Using Python with Selenium to navigate Python - cannot find link?

问题

I am an accountant and very novice coder. I am trying to use Selenium with Python and Firefox browser to click through a series of webpages. I am just trying to select and click a hyperlink on the page, but I can't seem to locate it with Selenium. Please can someone show me the simplest way to select the hyperlink I want?

Imports:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

I then have code which successfully navigates to a webpage, inputs my username and password and submits the form. Then when I want to click a link on the next page:

time.sleep(5) # to make sure webpage loads before trying to find link
Link1 = browser.find_element(By.partialLinkText("/maf/app/auth")) # or any easier selector method?
Link1.click()

The error message is "AttributeError: type object 'By' has no attribute 'partialLinkText'."

When I inspect the link element it looks like this - not sure what the easiest element is to locate and subsequently click it?

<a target="_blank" title="Go to Orbitax" href="/maf/app/authentication/sso/ping/redirect?target=orbitax_chre">
Go to Orbitax 
<img src="/cpw/images/rightBlueArrow.png" alt="" 
width="5" height="8"></a>

Thanks in advance!

英文:

I am an accountant and very novice coder. I am trying to use Selenium with Python and Firefox browser to click through a series of webpages. I am just trying to select and click a hyperlink on the page, but I can't seem to locate it with Selenium. Please can someone show me the simplest way to select the hyperlink I want?

Imports:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

I then have code which successfully nagivates to a webpage, inputs my username and password and submits the form. Then when I want to click a link on the next page:

time.sleep(5) # to make sure webpage loads before trying to find link
Link1 = browser.find_element(By.partialLinkText (&quot;/maf/app/auth&quot;)); # or any easier selector method?
Link1.click()

The error message is "AttributeError: type object 'By' has no attribute 'partialLinkText'"

When I inspect the link element it looks like this - not sure what the easiest element is to locate and subsequently click it?

&lt;a target=&quot;_blank&quot; title=&quot;Go to Orbitax&quot; href=&quot;/maf/app/authentication/sso/ping/redirect?target=orbitax_chre&quot;&gt;
Go to Orbitax 
&lt;img src=&quot;/cpw/images/rightBlueArrow.png&quot; alt=&quot;&gt;&quot; 
width=&quot;5&quot; height=&quot;8&quot;&gt;&lt;/a&gt;

Thanks in advance!

答案1

得分: 1

# 请注意以下部分已被翻译,代码部分未变更:

time.sleep(5) # 确保网页加载完成后再尝试查找链接
Link1 = browser.find_element(By.PARTIAL_LINK_TEXT, &quot;/maf/app/auth&quot;) # 或者是否有更简单的选择器方法?
Link1.click()
英文:

Your code snippet here

time.sleep(5) # to make sure webpage loads before trying to find link
Link1 = browser.find_element(By.partialLinkText (&quot;/maf/app/auth&quot;)); # or any easier selector method?
Link1.click()

There is no By.partialLinkText in the Selenium Python bindings. It should be

    time.sleep(5) # to make sure webpage loads before trying to find link
    Link1 = browser.find_element(By.PARTIAL_LINK_TEXT, &quot;/maf/app/auth&quot;); # or any easier selector method?
    Link1.click()

答案2

得分: 0

要翻译的内容:

要么你正在使用旧版本的selenium,要么import没有正常工作。

尝试将selenium更新到最新版本。

或者

如果你正在使用旧版本的Chrome,可以使用以下代码。

import time
from selenium.webdriver.common.by import By

time.sleep(5)
link1 = browser.find_element(By.LINK_TEXT, "转到Orbitax")
link1.click()
英文:

Either you are using old version of selenium or import is not work correctly.

Try to update the selenium to the latest version.

OR

use the below code if you are using an old version of chrome.

time.sleep(5)
link1 = browser.find_element(By.LINK_TEXT, &quot;Go to Orbitax&quot;)
link1.click()

huangapple
  • 本文由 发表于 2023年2月27日 18:01:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定