Element found but it's not clicked and the test fails

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

Element found but it's not clicked and the test fails

问题

I understand that you want the code-related text to be translated. Here's the translated code:

# 创建一个使用亚马逊网站的自动化测试,需要导航到“注册”页面。注册帐户的链接位于仅在鼠标悬停在登录链接上时才会弹出的菜单中。在终端中键入以下内容,即可获得所需结果:

# 选择元素
$$("a.nav-a[href*='https://www.amazon.com/ap/register']")

# 将其添加到`find_element()`方法中
driver.find_element(By.CSS_SELECTOR, "a.nav-a[href*='https://www.amazon.com/ap/register']").click()

# 页面加载,我已设置为在隐身模式下打开,我可以看到框架出现,就像在悬停时一样,但它没有导航到页面,我在这行代码上得到了一个错误指向。

Please note that the translation provided here is only for the code-related text, as per your request.

英文:

I'm creating an automated test using amazon's website and I have to navigate to the "sign up" page. The link to register an account is in a menu that only pops up when you hover over the sign in link. When I type the following in the terminal I get the result I need

$$("a.nav-a[href*='https://www.amazon.com/ap/register']")

So I added it a find_element() method like this

driver.find_element(By.CSS_SELECTOR, "a.nav-a[href*='https://www.amazon.com/ap/register']").click()

The page loads, which I have set to open in incognito mode, I can see the frame appear as it does when you hover over it, but it doesn't navigate to the page and I get an error pointing at this line of code.

I've also tried these

# searching from parent to child
driver.find_element(By.CSS_SELECTOR, 'div#nav-ya-flyout-ya-newCust a[href*="https://www.amazon.com/ap/register"]').click()

# searching by the text it contains
driver.find_element(By.CSS_SELECTOR, "a.nav-a[contains(text(), 'Start here.')]").click()

Both of these yield the same result, selenium seems to find it but doesn't click it. Does anybody know why this is happening?

答案1

得分: 0

好的,我明白了,我需要使用 ActionChains() 方法,并设置如下:

# 鼠标操作
mouse_actions = ActionChains(driver)

# 查找要悬停的登录链接
signin = driver.find_element(By.CSS_SELECTOR, "a#nav-link-accountList")

# 悬停在登录链接上
mouse_actions.move_to_element(signin).perform()

# 点击注册链接
driver.find_element(By.CSS_SELECTOR, 'a.nav-a[href*="https://www.amazon.com/ap/register"]').click()
英文:

Ok I figured it out, I needed to use the ActionChains() method and set it up like this

# Mouse Actions
mouse_actions = ActionChains(driver)

# Find Sign In link to hover over
signin = driver.find_element(By.CSS_SELECTOR, "a#nav-link-accountList")

# Hover over sign in link
mouse_actions.move_to_element(signin).perform()

# Click Registration link
driver.find_element(By.CSS_SELECTOR, 'a.nav-a[href*="https://www.amazon.com/ap/register"]').click()

huangapple
  • 本文由 发表于 2023年5月18日 06:17:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276528.html
匿名

发表评论

匿名网友

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

确定